Share via

Excel VB Power Query

Anonymous
2021-11-05T01:40:32+00:00

I am using excel 2019, Windows 10 and have imported 2 tables via Power Query. The resulting query shows in the Workbook Query area. When I merge the two tables via a power query merge query, the merge query also shows up in the Workbook Query area. Everything is as it should be. The problem is, in order to repetitively use the merge query in visual basic code, I have to delete the instance of the merge query using visual basic code and I can't find a way to delete the merge query. Can anyone help me?

Thanks

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2021-11-05T08:03:11+00:00

    If I assume that the name of the query is MyMerged:

    Sub Test()
    Dim Q As WorkbookQuery
    For Each Q In ThisWorkbook.Queries
    If Q.Name = "MyMerged" Then
    Q.Delete
    Exit For
    End If
    Next
    End Sub

    Andreas.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2021-11-05T14:04:28+00:00

    Thanks, this works perfectly with some minor changes based upon what I am trying to do.

    Thanks

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2021-11-05T14:00:38+00:00

    Thanks. This interesting. Not quite what I need but the "CommandBar" line looks interesting. Not sure I understand since I don't use the command bars alot but something to look into. Thanks

    Was this answer helpful?

    0 comments No comments
  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Anonymous
    2021-11-05T07:09:26+00:00

    Hi,

    Please check whether the following code is helpful.

    Please add the following code in Your existing code.

    Workbooks("Book3").Activate

    Worksheets("Sheet1").Activate

    Range("SalesReport[#All]").Select

    Application.CommandBars("Queries and Connections").Visible = Not Application.CommandBars("Queries and Connections").Visible

    Selection.ListObject.QueryTable.Delete

    Selection.ClearContents

    Modification 1: in the above code, please replace Book3 with the workbook name in which the merged query resides.

    Modification 2: in the above code, please replace Sheet1 with the name of the worksheet in which the merged query resides.

    Modification 3: in the above code, please replace SalesReport with the name of the merged query.

    Hope, this helped.

    Please respond if You require further assistance. I will try My Best to be of help.

    If I was able to help You, please mark My response as answer and helpful.

    Thank You!

    Was this answer helpful?

    0 comments No comments