+ Reply to Thread
Results 1 to 16 of 16

how to set up macro to select newly created work sheet?

  1. #1
    Registered User
    Join Date
    03-20-2012
    Location
    usa
    MS-Off Ver
    Microsoft 365 MSO Version 2304
    Posts
    39

    Question how to set up macro to select newly created work sheet?

    greetings and good morning all, i'm in the process of trying to create a macro where I have multiple worksheets and would need to have the macro setup where when it runs, it automatically goes to the newest created worksheet name then does it's thing (which is update and revise a few pivot tables). for example, in VBA under the VBAProject window pane, theres 15 sheets, (Sheet1, Sheet2.....Sheet15). if i'm on Sheet1 and want the macro to go straight to the latest/newly created sheet name, sheet 15 in this case. later when I create a new sheet name, sheet 16 then the macro would automatically go there if its in a different sheet name. thanks in advance!

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: how to set up macro to select newly created work sheet?

    Assuming that it's always going to be newly added sheet, and it's not code that's creating new sheet... then something like below.
    Please Login or Register  to view this content.
    If code is creating new sheet, then Worksheet object variable should be set at the time of creation.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    03-20-2012
    Location
    usa
    MS-Off Ver
    Microsoft 365 MSO Version 2304
    Posts
    39

    Re: how to set up macro to select newly created work sheet?

    I do have a code that creates a new worksheet name:
    PHP Code: 
    Sub step1()
    '
    step1 Macro

        Cells
    .Select
        Selection
    .Copy
        Sheets
    .Add
        ActiveSheet
    .Name "MRFE Scuff Height  " & (Format(Now"mm.dd.yy"))
        
    ActiveSheet.Paste
        ActiveWindow
    .Zoom 90
        ActiveWindow
    .Zoom 80
         Range
    ("A1") = Format(Date"mm/dd/yy")
        
    Range("E1").Select

    End Sub 

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: how to set up macro to select newly created work sheet?

    Then when you do "Sheets.Add" set it as your worksheet object.

    Ex:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    03-20-2012
    Location
    usa
    MS-Off Ver
    Microsoft 365 MSO Version 2304
    Posts
    39

    Re: how to set up macro to select newly created work sheet?

    I think it worked lol. the pivot tables that I have in my step2 macro did update to the new worksheet. I had thought the call for the demo function would go to the newly created worksheet but it stayed on the worksheet name I was on, whicih was worksheet1.

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: how to set up macro to select newly created work sheet?

    Demo is just a code to demonstrate the concept

    If you want to go to the new sheet, you just have to replace Debug.Print line with ws.Select .

  7. #7
    Registered User
    Join Date
    03-20-2012
    Location
    usa
    MS-Off Ver
    Microsoft 365 MSO Version 2304
    Posts
    39

    Re: how to set up macro to select newly created work sheet?

    ohhhhhhhh. im still a noobie lol thanks CK76

  8. #8
    Registered User
    Join Date
    03-20-2012
    Location
    usa
    MS-Off Ver
    Microsoft 365 MSO Version 2304
    Posts
    39

    Re: how to set up macro to select newly created work sheet?

    ok I changed the Debug.Print ws.Name to ws.Select ws.Name and got an error saying "Run-time '1004': Method 'Select' of object'_Worksheet' failed
    when I highlight line where the error accorded, it pointed to the very last worksheet in the file.

  9. #9
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: how to set up macro to select newly created work sheet?

    Can you upload sample and how you are using the code?

    If you just used my demo code and replaced Debug.Print line with ws.Select it will not throw that error.

    To upload, use "Go Advanced" button and follow "Manage Attachments" hyperlink. It will launch new tab/window for managing uploads.

  10. #10
    Registered User
    Join Date
    03-20-2012
    Location
    usa
    MS-Off Ver
    Microsoft 365 MSO Version 2304
    Posts
    39

    Re: how to set up macro to select newly created work sheet?

    I would but since this is for work its sensitive material and I don't feel comfy about uploading it on the message forum. the workbook has a total of 15 sheet names and they're not all in numerical order.

  11. #11
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: how to set up macro to select newly created work sheet?

    Replace all sensitive data with something generic. With only enough data and objects to demonstrate how you are using the workbook.

    From there I can deduce what sort of code change is needed.

  12. #12
    Registered User
    Join Date
    03-20-2012
    Location
    usa
    MS-Off Ver
    Microsoft 365 MSO Version 2304
    Posts
    39

    Re: how to set up macro to select newly created work sheet?

    see attachment.

  13. #13
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: how to set up macro to select newly created work sheet?

    It isn't clear to me, which sheet that you are trying to activate. Is it Sheet15?

    Or is it some other sheet?

  14. #14
    Registered User
    Join Date
    03-20-2012
    Location
    usa
    MS-Off Ver
    Microsoft 365 MSO Version 2304
    Posts
    39

    Re: how to set up macro to select newly created work sheet?

    in this case, sheet15, but when it creates a new sheet, sheet16, I want the macro to highlight sheet16.

  15. #15
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: how to set up macro to select newly created work sheet?

    As I under stand it, you are running Step 1 then step 2...

    So step 1 already selects the added sheet.

    You really wouldn't need to select step 2. Unless you are doing something manually in between these two steps and different sheet is active.

    If that is the case, you'd want to use public variable.

    Ex: You can delete the Demo() sub.
    Please Login or Register  to view this content.
    This will work as long as the workbook isn't closed between running step 1 and step 2.

    See attached.

  16. #16
    Registered User
    Join Date
    03-20-2012
    Location
    usa
    MS-Off Ver
    Microsoft 365 MSO Version 2304
    Posts
    39

    Re: how to set up macro to select newly created work sheet?

    i'll take a look. thanks CK76

+ 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. macro to add conditional formatting to a newly created sheet
    By Shellybelly in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-27-2015, 09:09 AM
  2. [SOLVED] macro to copy paste visible cells into a newly created sheet
    By melody10 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-19-2015, 04:14 PM
  3. Replies: 4
    Last Post: 09-18-2014, 01:05 PM
  4. [SOLVED] How to create macro to copy select data, or row into newly created tabs
    By Dariusd7 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-14-2013, 12:01 PM
  5. Add vba to current macro to copy table into newly created sheet
    By Morgraal in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-15-2013, 07:10 AM
  6. Getting Macro to reference newly created sheet
    By creuther in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-27-2011, 01:04 AM
  7. macro that will automatically link a newly created worksheet to a summary sheet
    By liquidblack in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-18-2009, 03:22 PM

Tags for this Thread

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