+ Reply to Thread
Results 1 to 15 of 15

Activate this worksheet

  1. #1
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Activate this worksheet

    Hi all, I have 5 worksheets all the similar with the same worksheet VBA code.

    I need the bit of code to activate this worksheet before a macro is run.

    Say for instance sheet 3 is the active one, but the code running on sheet 4 has triggered the macro to run - I need to insert something into sheet 4 code to activate that sheet (instead of sheet 3).

    Something like this.worksheet = activate ???

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Activate this worksheet

    Why do you need to activate Sheet3?

    Can't you reference Sheet3 in the code?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Activate this worksheet

    I'm using sheet 3 as the example sheet that was active (selected) at that moment in time.

    I'm basically saying is there a way of having code as below, instead of having to change the sheet name for each worksheet (Set ws = sheet1, set ws = sheet 2 etc etc).

    Please Login or Register  to view this content.
    I quite often create more worksheets by copying existing ones, this means having to change the sheet number in the VBA code each time - if I forget, the code is referencing the wrong worksheet

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Activate this worksheet

    I'm confused, which sheet(s) do you want this code to run on?

    Also, where is the code triggered and where is it located?

  5. #5
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Activate this worksheet

    The code is built into each worksheet Private Sub Worksheet_Calculate()

    Each worksheet is running its on code. The macro is triggered if cell A1 > 10

    So say for instance I have sheet 3 active, and cell A1 = 8 the code should not run. BUT on sheet 4, A1 = 12 - it will call for the macro to run. But the macro will run on sheet 3 (as its the active one). So I am saying I need some code that will activate sheet 4 before running the macro - but I want to do this without specifically typing sheet 4 in the coding (for reasons mentioned above).

    There must be a bit of code that will activate a worksheet without having to reference it in Sheet 4, sheet 3 or whatever terms??

  6. #6
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Activate this worksheet

    The code is built into each worksheet Private Sub Worksheet_Calculate()

    Each worksheet is running its on code. The macro is triggered if cell A1 > 10

    So say for instance I have sheet 3 active, and cell A1 = 8 the code should not run. BUT on sheet 4, A1 = 12 - it will call for the macro to run. But the macro will run on sheet 3 (as its the active one). So I am saying I need some code that will activate sheet 4 before running the macro - but I want to do this without specifically typing sheet 4 in the coding (for reasons mentioned above).

    There must be a bit of code that will activate a worksheet without having to reference it in Sheet 4, sheet 3 or whatever terms??

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Activate this worksheet

    Is the code for each worksheet the same?

    If it is then you might want to look at the SheetCalculate workbook level event.
    Please Login or Register  to view this content.
    As you can see it's passed a parameter, Sh, which is the sheet that has been calculated.

  8. #8
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Activate this worksheet

    Yes the code is the same, ok will try that and let you know

  9. #9
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Activate this worksheet

    Unfortunately this doesn't work, the code doesn't run at all

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Activate this worksheet

    Are you sure?

    Can you post what you tried?

  11. #11
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Activate this worksheet

    Sure

    Please Login or Register  to view this content.
    I then made A1 = (a cell on another sheet). I changed this cell to 12 - and nothing happened (so macro1 wasn't called and the other sheet wasn't selected)

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Activate this worksheet

    Again a little confused.

    Is Macro1 the macro you want to run on each sheet?

    Can you post the code for it?

  13. #13
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Activate this worksheet

    Yes Macro 1 is the code to run. It is below but not really relevant as the above code doesn't even get to that stage

    Please Login or Register  to view this content.

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Activate this worksheet

    Where did you put the code from post #11?


    It should go in the ThisWorkbook module and should look something like this.
    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    710

    Re: Activate this worksheet

    AH ok, I was putting it on the sheet code. Will try again

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA to activate the other worksheet
    By impresxy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2013, 04:38 AM
  2. Replies: 3
    Last Post: 10-10-2012, 05:10 PM
  3. Worksheet Change and Worksheet Activate Events Reprotecting Automatically
    By excelnewb02 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-21-2012, 08:44 PM
  4. [SOLVED] Activate Previous worksheet after adding a new worksheet
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-18-2005, 08:05 PM
  5. [SOLVED] Activate Worksheet
    By cottage6 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-21-2005, 02:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1