Passing a parameter from VBAto Power Query

peterhess2002

New Member
Joined
Mar 29, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello!

I Know that this subject has been discussed before many times.

I was not able to figure out through the threads who to fix my problem.

I have a Paramter predefined in Power Query name it ParameterX.

I want to put a value in the ParamterX from VBA.

I want to pass a different value for different sheets to every ParameterX (a string) in every sheet.

Which code is necessery in VBA and which one in Power Query?

Any Idea?

Regards

Peter
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The question is just to pass a parameterY in VBA to ParameterX in Pwoer Query for an active sheet.
 
Upvote 0
Isn't this as simple as something like this:
VBA Code:
Dim q As QueryTable
Set q = ActiveWorkbook.Queries(1)
q.Parameters(1).SetParam xlConstant, 123
 
Upvote 0
Hello and thanks for your answer.

I am trying to generate multiple sheets depending on a name list present in a List loaded from a file.

My code looks like this:

---------------

Sub Test_1()
Dim i, LastFileNum, New_Position, Length_Name, Length_Name_New As Long
Dim Actual_Name, Actual_Name_New, gesucht1, gesucht2 As String
Dim qry As WorkbookQuery

gesucht1 = "Knicken_"
gesucht2 = "Knicken_z_stk"

' Sheets("FileX").Activate

LastFileNum = 11
New_Position = 0

For i = 9 To LastFileNum

Actual_Name = Sheets("FileX").Cells(i, 1)
Length_Name = Len(Actual_Name)

If Length_Name = 0 Then
MsgBox i
Exit For
End If

Length_Name_New = Length_Name - 4
Actual_Name_New = Left(Actual_Name, Length_Name_New)

MyPos1 = InStr(1, Actual_Name, gesucht1)
MyPos2 = InStr(1, Actual_Name, gesucht2)

New_Position = New_Position + 1

If MyPos1 = 1 Then
If MyPos2 = 1 Then
Sheets("Knicken_Z").Copy After:=Sheets(New_Position)
ActiveSheet.Name = Actual_Name_New
ActiveSheet.ListObjects(1).Name = Actual_Name_New

Dim q As QueryTable
Set q = ActiveWorkbook.Queries(1)
q.Parameters(1).SetParam xlConstant, 123


'Set XXX = ActiveSheet.ListObjects(1)
'MsgBox XXX

ElseIf MyPos2 = 0 Then
Sheets("Knicken").Copy After:=Sheets(New_Position)
ActiveSheet.Name = Actual_Name_New
ActiveSheet.ListObjects(1).Name = Actual_Name_New
End If
Else
Length_Name_New = Length_Name_New - 12
Actual_Name_New = Right(Actual_Name_New, Length_Name_New)
Sheets("AugeX").Copy After:=Sheets(New_Position)
ActiveSheet.Name = Actual_Name_New
ActiveSheet.ListObjects(1).Name = Actual_Name_New
End If
Next i
End Sub
---------------------------

The code works just fine and do the job.

I just want to let a ParameterX in Powerquery have every time a different value depending on Actual_Name in may code.

I want to use the ParameterX to define the CSV file schould be readed by the query.

It is namly every time different...

The red marked code is what you suggest and it did not works.
 
Upvote 0
You should replace the 123 in my code with the variable you want to pass as a parameter. In addition, my code addresses both the query and the parameter by index number. You should consider to change that to the name of the query and the name of the parameter respectively if either of those two might not be the first or if you do not know beforehand how many of them there will be..
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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