Green Squirrel
New Member
- Joined
- Jan 9, 2021
- Messages
- 25
- Office Version
- 365
- Platform
- MacOS
I have a vba script to get tables form a website and put them in Excel.
As you can see I get the values for TBL, URL, destCell and .WebTables from a different sheet. So these are variable. Reason for me doing this is that I want to use this script multiple times.
The final version will have about 16 colums with each having 4 rows.
So Instead for making 16 odd subs I want make a script that runs the data and use this data in my script above.
But I haven't got a clue where to start.
Tips and hints most welcome.
VBA Code:
Public Sub ImportTBL()
Dim destCell As Range
Dim QT As QueryTable
Dim qtResultRange As Range
Dim URL As String
Dim sourceSheet As Worksheet
Dim TBL As String
Set sourceSheet = Sheet2
TBL = ThisWorkbook.Sheets(1).Range("A1")
URL = ThisWorkbook.Sheets(1).Range("A2")
With sourceSheet
Set destCell = .Range(ThisWorkbook.Sheets(1).Range("A3"))
On Error Resume Next
.ListObjects(TBL).Delete
On Error GoTo 0
End With
Set QT = destCell.Worksheet.QueryTables.Add(Connection:="URL;" & URL, Destination:=destCell)
With QT
.RefreshStyle = xlOverwriteCells
.WebFormatting = xlNone
.WebSelectionType = xlSpecifiedTables
.WebTables = ThisWorkbook.Sheets(1).Range("A4")
.BackgroundQuery = False
.Refresh
Set qtResultRange = .ResultRange
.Delete
End With
With destCell
.Worksheet.ListObjects.Add(xlSrcRange, .CurrentRegion, , xlYes).Name = TBL
sourceSheet.ListObjects(TBL).ShowAutoFilterDropDown = False
End With
End Sub
As you can see I get the values for TBL, URL, destCell and .WebTables from a different sheet. So these are variable. Reason for me doing this is that I want to use this script multiple times.
The final version will have about 16 colums with each having 4 rows.
So Instead for making 16 odd subs I want make a script that runs the data and use this data in my script above.
But I haven't got a clue where to start.
Tips and hints most welcome.