Update Query Filter with VBA

kaja30

New Member
Joined
Jan 31, 2020
Messages
3
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hello,

Is it possible to write a VBA code that would change filter in the existing query. The code I gave so far is not working, mCode variable id returning code that is not the same as in the advanced editor in the Power Query Editor the source is different. When I run the query for the second time the mCode variable shows the filter changed but in reality the filter has changed. Please help

Sub FilterPowerQuery()
Dim wb As Workbook
Dim query As WorkbookQuery
Dim queryName As String
Dim NewCode As String
Dim mcode As String
Dim Source As String

On Error GoTo ErrorHandler
Set wb = ThisWorkbook
queryName = "Fruits"

Set query = wb.Queries(queryName)
mcode = query.Formula

NewCode = Replace(mcode, """Apples""", """Bananas""")

query.Formula = NewCode

Exit Sub

ErrorHandler:
MsgBox "An error occurred: " & Err.Description, vbExclamation

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hello,

Is it possible to write a VBA code that would change filter in the existing query. The code I gave so far is not working, mCode variable id returning code that is not the same as in the advanced editor in the Power Query Editor the source is different. When I run the query for the second time the mCode variable shows the filter changed but in reality the filter has changed. Please help

Sub FilterPowerQuery()
Dim wb As Workbook
Dim query As WorkbookQuery
Dim queryName As String
Dim NewCode As String
Dim mcode As String
Dim Source As String

On Error GoTo ErrorHandler
Set wb = ThisWorkbook
queryName = "Fruits"

Set query = wb.Queries(queryName)
mcode = query.Formula

NewCode = Replace(mcode, """Apples""", """Bananas""")

query.Formula = NewCode

Exit Sub

ErrorHandler:
MsgBox "An error occurred: " & Err.Description, vbExclamation

End Sub
Also query.Refresh is causing an Object defined error
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top