Hello,
I am using Get & Transform in a VBA routine to get the contents of a webpage that I want to later parse. The content is bills from our state legislature and how the legislators voted. Each Bill has its own page, so I want to build the string according to the bill number and use the string variable to get the contents. Below is the code I am trying to use. The first section is the code, where you can see the string variable is not resolving. The second part is when I recorded a macro using the same exact string, which works fine. I'm not real good at determining the format for strings, so if you could guide me, that would be great.
I am using Get & Transform in a VBA routine to get the contents of a webpage that I want to later parse. The content is bills from our state legislature and how the legislators voted. Each Bill has its own page, so I want to build the string according to the bill number and use the string variable to get the contents. Below is the code I am trying to use. The first section is the code, where you can see the string variable is not resolving. The second part is when I recorded a macro using the same exact string, which works fine. I'm not real good at determining the format for strings, so if you could guide me, that would be great.
VBA Code:
Sub GetBillInfoFromWeb()
'
' Macro3 Macro
'
'
Dim BillNum As String
Dim WebAddress As String
[COLOR=rgb(250, 197, 28)]WebAddress[/COLOR] = "https://legislature.idaho.gov/sessioninfo/2023/legislation/"
Dim RowNum As Integer
Dim ColNum As Integer
Dim scratch As String
RowNum = 4
Sheets("LegislatureVotes").Select
BillNum = "Start"
Do Until BillNum = ""
BillNum = Cells(RowNum, 1).Value2
WebAddress = WebAddress & BillNum & "/"
Sheets("Scratchpad").Select
ActiveWorkbook.Queries.Add Name:="Table 0", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents([COLOR=rgb(250, 197, 28)]webaddress[/COLOR]))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Column1"", type text}, {""Column2"", type date}, {""Column3"", type text}, {""Column4"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 0"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table 0]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_0"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Loop
End Sub
VBA Code:
Sub Macro6()
'
' Macro6 Macro
'
'
ActiveWorkbook.Queries.Add Name:="Table 0", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents([COLOR=rgb(250, 197, 28)]""https://legislature.idaho.gov/sessioninfo/2023/legislation/H0001/""[/COLOR]))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Column1"", type text}, {""Column2"", type date}, {""Column3"", type text}, {""Column4"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 0"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table 0]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_0"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
End Sub