VBA Web query - How can I specify destination to named sheet

suprsnipes

Active Member
Joined
Apr 26, 2009
Messages
434
Hi,

I have a web query created using VBA and would like to know how to change the destination from ActiveSheet to specific sheet name? In this case "bar".

Code:
With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;" & MyUrl, Destination:=Cells(1, 1))
    .PostText = MyPost
    .RefreshStyle = xlOverwriteCells
    .PreserveFormatting = True
    .AdjustColumnWidth = False
    .WebFormatting = None
    .BackgroundQuery = True
    .TablesOnlyFromHTML = True
    .Refresh BackgroundQuery:=False
    .SaveData = True
End With

suprsnipes
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Thanks Norie ... where does the code go?

Worksheets("bar")
It replaces ActiveSheet.

There's nothing else I can see in the posted code that need changes.

I could have missed something of course, and if there is more code it could make a difference.
 
Upvote 0
This did the trick. Thanks.

Code:
Sheets("bar").Select
With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;" & MyUrl, Destination:=Cells(1, 1))
    .PostText = MyPost
    .RefreshStyle = xlOverwriteCells
    .PreserveFormatting = True
    .AdjustColumnWidth = False
    .WebFormatting = None
    .BackgroundQuery = True
    .TablesOnlyFromHTML = True
    .Refresh BackgroundQuery:=False
    .SaveData = True
End With
 
Upvote 0
Did you try replacing ActiveSheet with Worksheets("bar")?
 
Upvote 0
Yes I did, but received an error. Tried it again just now with no problems so I must have made a mistake. Appreciate your help.
 
Upvote 0
I think I know what the problem was, and I did sort of notice it earlier but wasn't sure.

Anyway there's something else that needs to be changed - Cells(1,1).

Without a worksheet reference that will refer to the active worksheet, so if you explicitly tell VBA to place the query table on the worksheet 'bar' and
that isn't active then you'll get an error.

Anyway, replace Cells(1,1) with Worksheets("Bar").Cells(1,1) and it should work no matter what worksheet is active.
 
Upvote 0

Forum statistics

Threads
1,224,575
Messages
6,179,637
Members
452,934
Latest member
Jdsonne31

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