Using string for Web Query connection

Roderick_E

Well-known Member
Joined
Oct 13, 2007
Messages
2,051
I can't figure out why I can't make a dynamic string for this web query. It is supposed to update an existing query table with a different currency code. If I change the code manually it works. If I remove the roestr from the red line and simply change the exchange code, gbp to eur or jpy or something, it works but if I try to control the code with the roestr method I get popup error 1004 on the .connection line. Any ideas?

Code:
Sub docurquery()
Dim roestr As String
roecode = Sheet2.Range("D1") 'any currency code such as EUR
roestr = """URL;http://www.x-rates.com/table/?from=" & roecode & "&amount=1"""
    With ActiveWorkbook.Connections("Connection")
        .Name = "Connection"
        .Description = ""
    End With
    Sheet1.Activate
    Sheet1.Range("A:C").Select
    With Selection.QueryTable
        .Connection = roestr [B][COLOR=#ff0000]'"URL;http://www.x-rates.com/table/?from=gbp&amount=1"[/COLOR][/B]
        '.CommandType = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    ActiveWorkbook.Connections("Connection").Refresh
    
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
SOLVED:

It didn't want the quotes:

Code:
Sub docurquery()
Dim roestr As String
Dim roecode As String
roecode = Sheet2.Range("D1") 'any currency code such as EUR
roestr = [COLOR=#ff0000]"URL;http://www.x-rates.com/table/?from=" & roecode & "&amount=1"[/COLOR]
    With ActiveWorkbook.Connections("Connection")
        .Name = "Connection"
        .Description = ""
    End With
    'MsgBox roestr
    'Exit Sub
    Sheet1.Activate
    Sheet1.Range("A:C").Select
    With Selection.QueryTable
        .Connection = roestr '"URL;http://www.x-rates.com/table/?from=gbp&amount=1"
        '.CommandType = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    ActiveWorkbook.Connections("Connection").Refresh
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,379
Messages
6,124,608
Members
449,174
Latest member
ExcelfromGermany

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