Hi, I'm relatively new to VBA and have some questions regarding web queries in my code.
I'm pulling three different tables from the same site, and the only differences in the URL is the year for each. Each table is to go to a different sheet. As of right now, I've simply cut and pasted the same code three times, replacing the destination range and the year in the URL to get the outcome I want.
So I have the following:
I repeat this three times and just swap in the next worksheet and replace Year1 with Year2...and then Year3.
It works just fine but it seems less than efficient. I'd like to learn how to loop this, but I can't seem to find an answer on the web as to how to implement a loop when I have two variables changing together. I'm not sure how I would define an array or collection or if that would even be the best way to go about it.
It's just three queries, but I'll be adding some more and some of those may introduce some additional variables, so I want to make sure I'm doing this right from the get go. I'll save any additional questions for later.
Thanks
I'm pulling three different tables from the same site, and the only differences in the URL is the year for each. Each table is to go to a different sheet. As of right now, I've simply cut and pasted the same code three times, replacing the destination range and the year in the URL to get the outcome I want.
So I have the following:
Code:
Year1 = Worksheets("Records").Range("F25")
Year2 = Worksheets("Records").Range("F24")
Year3 = Worksheets("Records").Range("F23")
With Worksheets("[B]IndHitting[/B]").QueryTables.Add(Connection:= _
"URL;http://www.baseball-reference.com/leagues/MLB/" & [B]Year1[/B] & "-standard-batting.shtml#players_standard_batting::none" _
, Destination:=Worksheets("[B]IndHitting[/B]").Range("$A$1"))
.Name = "none"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0#
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = """players_standard_batting""
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = True
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
I repeat this three times and just swap in the next worksheet and replace Year1 with Year2...and then Year3.
It works just fine but it seems less than efficient. I'd like to learn how to loop this, but I can't seem to find an answer on the web as to how to implement a loop when I have two variables changing together. I'm not sure how I would define an array or collection or if that would even be the best way to go about it.
It's just three queries, but I'll be adding some more and some of those may introduce some additional variables, so I want to make sure I'm doing this right from the get go. I'll save any additional questions for later.
Thanks
Last edited: