Recording VBA to change SQL Statement

lhershco

New Member
Joined
Jun 29, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I am trying to record a VBA that will automate a specific SQL code like in all my power queries SQL statements.
in all of them i have a version number and i would like to have an automation that once i pick a version number it will insert that number to all power queries SQL statements.
The SQL code line is currently WHERE PD.PublishLogId = 52 in all power queries SQL statements and i want the to run a macro that will ask me which PublishLogId number i want it to be and then replace the 52 (or any other number currently there) with that new number i picked.
when i record a macro it ignores all my clicks and no VBA code is written even though i am recording .
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
VBA code to use an inputbox example is shown here. I have used a msgbox to return the number I tested when the inputbox ran. I don't know if this will help with you Power SQL queries though.

VBA Code:
Sub intb()
Dim i As Integer
i = InputBox("Title", "Please enter your number")
'WHERE PD.PublishLogId = i
MsgBox "Integer number is " & i
End Sub
 
Upvote 0
Welcome to MrExcel forums.

Try this macro on a copy of your workbook.
VBA Code:
Public Sub Modify_SQL()

    Dim ws As Worksheet
    Dim listObj As ListObject
    Dim id As String
    
    id = InputBox("Enter Id number")
    
    If id <> "" Then
        For Each ws In ActiveWorkbook.Worksheets
            For Each listObj In ws.ListObjects
                listObj.QueryTable.CommandText = Left(listObj.QueryTable.CommandText, InStr(listObj.QueryTable.CommandText, "= ") + 1) & id
                listObj.QueryTable.Refresh
            Next
        Next
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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