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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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