How to add queries to new sheets depending on the name? On VBA Excel

jc1217

New Member
Joined
Jan 11, 2018
Messages
1
I was wondering how I would go about making a macro which reads off a list of specific URL variables and makes a data query and a new sheet for a different name.
For example in my url tab I have this:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">Dan Butler 2017
Dan Butler
2018
Jack Riewoldt
2007
Jack Riewoldt
2008
Jack Riewoldt
2009
Jack Riewoldt
2010
Jack Riewoldt
2011
Jack Riewoldt
2012
Jack Riewoldt
2013
Jack Riewoldt
2014
Jack Riewoldt
2015
Jack Riewoldt
2016
Jack Riewoldt
2017
Jack Riewoldt
2018</code>The URL I have is this: http://www.fanfooty.com.au/players/year.php?firstname=Dylan&surname=Grimes&year=2018
As you can see the only variables are the first and last name and the year.
I want it so the macro will make a new sheet for each new name (so for example Dan Butler will have his own tab with 2017 and 2018 stats added in to it, Jack Riewoldt has his own tab with 2007-18 stats added into it) and add tables to that sheet for each year. It should only make a new sheet for each new name it encounters.
What I currently have is this:

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">Option Explicit

Public Sub GetAllUrls()
Dim wsURL As Worksheet
Set wsURL = ThisWorkbook.Worksheets("URL") 'here we define the urls worksheet

Dim i As Long
For i = 1 To 14 'we assume the data is in A1 to C14 (A=name, B=surname, C=year)
AddConnection wsURL
.Cells(i, 1), wsURL.Cells(i, 2), wsURL.Cells(i, 3)
Next i
End Sub

Public Sub AddConnection(Name, Surname, Year)
Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets.add(After:=ActiveSheet) 'add a new sheet
ws
.Name = Left(Name & " " & Surname & " " & Year, 31) 'rename sheet (sheet names must be max 31 characters)

With ws.QueryTables.add(Connection:= _
"URL;http://www.fanfooty.com.au/players/year.php?firstname=" & Name & "&surname=" & Surname & "&year=" & Year _
, Destination:=Range("$A$1"))
.Name = Name & " " & Surname & " " & Year
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub</code>

</pre>So far the only thing I could get is one sheet for every year and name, So it makes a new tab for dan butler 2017 and dan butler 2018, however i want it so dan butlers stats appear only in one sheet and makes a new sheet once it hits jack riewoldt
Thanks guys
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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