Macro Button on Diff WS

Landzir101

Board Regular
Joined
Jun 21, 2009
Messages
78
Fairly new to macros and I was how I would go about having a web query (that is executed by a button) execute on a separate worksheet than the active sheet it is on. This is a brief summary of what I am doing:

I have about a dozen worksheets that are formatted for a specific web query. On each page I have a macro assigned to a button. So, to get the data I go through each tab and click the button; it pulls the data through.

What I am wanting is a way to have the button separate from the worksheet (say on the main page) and be able to query all the data from that page. Not sure on the exact naming convention or way to do this. Thanks for the help!

Below is a little of the code so you can see what I am saying...

...
Sub Annual_Income_Statement()
Dim sSymbol As String
Dim sDestination As String

With ActiveSheet
.Range("B4:M200").ClearContents
sSymbol = .Range("B3").Value
sDestination = .Range("B3").Offset(2, 0).Address
With .QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q/is?s=" & sSymbol & "&annual", Destination:=Range(sDestination))
'.PostText = "Quote0=" & sSymbols
.Name = "Recent News " & sSymbol
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hello and welcome to MrExcel.

Instead of

Code:
With ActiveSheet

try

Code:
With Sheets("Sheet1")

Change Sheet1 to the name of the sheet.
 
Upvote 0
It is throwing an error saying that the destination range is not on the same worksheet that the Query table is being created on....help on how to code the destination range to the sheet name, cause I only have it directed to the active sheet currently right?
 
Upvote 0
Something like this Put the code in a standard module and teh button on a sheet named "Main". Queries all teh sheets
Code:
Option Explicit
Sub Annual_Income_Statement()
    Dim sSymbol As String
    Dim sDestination As String
    Dim ws As Worksheet
    Application.ScreenUpdating = False
    For Each ws In Worksheets
        If ws.Name <> "Main" Then
            ws.Select
            With ActiveSheet
                .Range("B4:M200").ClearContents
                sSymbol = .Range("B3").Value
                sDestination = .Range("B3").Offset(2, 0).Address
                With .QueryTables.Add(Connection:= _
                                      "URL;http://finance.yahoo.com/q/is?s=" & sSymbol & "&annual", Destination:=Range(sDestination))
                    '.PostText = "Quote0=" & sSymbols
                    .Name = "Recent News " & sSymbol
                End With
            End With
        End If
    Next ws
    Sheets("Main").Select
End Sub
 
Last edited:
Upvote 0
This should work - the dots qualify the ranges to Sheet1

Rich (BB code):
With Sheets("Sheet1")
    .Range("B4:M200").ClearContents
    sSymbol = .Range("B3").Value
    sDestination = .Range("B3").Offset(2, 0).Address
    With .QueryTables.Add(Connection:= _
        "URL;http://finance.yahoo.com/q/is?s=" & sSymbol & "&annual", Destination:=Range(sDestination))
        '.PostText = "Quote0=" & sSymbols
        .Name = "Recent News " & sSymbol
 
Upvote 0
Worked perfectly guys....but now i'm trying to go a little further into the process. I was wondering how/if you can make a button from one workbook that updates all the others (or at least some of them).

What I have: I have 420 different workbooks that each have buttons in them that update the worksheets in their respective workbooks. I need to update these once a quarter about. I was wanting to set up a MAJOR button that from one master workbook I can click and it goes about updating all the 420 others.

Is this possible? If it is, I am interested to know the key it (like defining workbooks within the macro button, etc.) and if any of my code so far would help I would be more than happy to post and work with you guys! Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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