use a generated URL in VBA not a static URL

StuartKStout

New Member
Joined
Mar 28, 2017
Messages
10
I am having a problem pulling a URL that is generated in one area of a spreadsheet 'particular cell' and using it in some VBA code,
Typing the URL into the VBA statically works fine, but I dont seem to be able to get the VBA to use the generated URL

:confused:
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How are you trying to get/use the URL?
 
Upvote 0
At the moment its just statically typed



Sub gethtmltableurl()
Dim objWeb As QueryTable
Dim sWebTable As String

' Clearsheet
Sheets("URLs").Select
Range("F16:N76").Select
Selection.Clear
Range("G14:H14").Select

'You have to count down the tables on the URL listed in your query

sWebTable = 1

'Sets the url to run the query and the destination in the excel file

Set objWeb = ActiveSheet.QueryTables.Add( _
Connection:="URL;https://www.swimmingresults.org/eventrankings/eventrankings.php?Pool=L&Stroke=1&Sex=M&TargetYear=S&AgeGroup=1718&AgeAt=D&StartNumber=1&RecordsToView=60&Level=N&TargetNationality=A&TargetRegion=P&TargetCounty=XXXX&TargetClub=XXXX", _
Destination:=Range("F16"))

With objWeb

.WebSelectionType = xlSpecifiedTables
.WebTables = sWebTable
.Refresh BackgroundQuery:=False
.SaveData = True
End With
Set objWeb = Nothing

End Sub
 
Upvote 0
How are you trying to use the value from the cell in the code?
 
Upvote 0
What exactly is in the cell 'generating' the URL?

Is it just the URL as a string or is it an actual hyperlink?
 
Upvote 0
Hi Norie

Sorry for not being clear, in cell G2 I am using the formula =HYPERLINK(C4) so its the contents of G2 that I need to use. new to VBA and its a whole new world.
 
Upvote 0
What happens with this code?
Code:
Sub gethtmltableurl()
Dim objWeb As QueryTable
Dim sWebTable As String
Dim strURL As String

    ' Clearsheet
    Sheets("URLs").Select
    Range("F16:N76").Select
    Selection.Clear
    Range("G14:H14").Select

    ' get URL from G2
    strURL = Range("G2").Value

    'You have to count down the tables on the URL listed in your query

    sWebTable = 1

    'Sets the url to run the query and the destination in the excel file

    Set objWeb = ActiveSheet.QueryTables.Add( _
                 Connection:="URL;" & strURL, _
                 Destination:=Range("F16"))

    With objWeb
        .WebSelectionType = xlSpecifiedTables
        .WebTables = sWebTable
        .Refresh BackgroundQuery:=False
        .SaveData = True
    End With
    
    Set objWeb = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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