+ Reply to Thread
Results 1 to 8 of 8

Button On Different Worksheet Than Where I Want The Code To Run

Hybrid View

  1. #1
    Registered User
    Join Date
    02-08-2017
    Location
    Virginia
    MS-Off Ver
    2010
    Posts
    77

    Button On Different Worksheet Than Where I Want The Code To Run

    Hello All,

    Thank you in advance for any help that you can provide. I'm hoping that my questions is relatively basic, but for some reason I cannot find a solution to my problem online.

    I want to have an Overview worksheet that has buttons to run certain macros on certain sheets. For example, I would like to push Button1 on the Summary Worksheet, and have it run my Macro on the "Import" worksheet.

    Is this as simple as I am hoping?

    Thank you!

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,027

    Re: Button On Different Worksheet Than Where I Want The Code To Run

    You can do that by having the macro reference the "Import" worksheet. For example, if you assign the macro below to your button, it will copy A1 from "Import" to A1 of the sheet with the button.
    Sub test()
        Sheets("Import").Range("A1").Copy Range("A1")
    End Sub
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    02-08-2017
    Location
    Virginia
    MS-Off Ver
    2010
    Posts
    77

    Re: Button On Different Worksheet Than Where I Want The Code To Run

    Thank you for the reply! Maybe I am misunderstanding you but I don't want the data from Import to be copied to the worksheet with the buttons. I want the macro to run on the Import worksheet, leave it on the import worksheet, but have the buttons on another worksheet. Or was I misunderstanding you?

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,027

    Re: Button On Different Worksheet Than Where I Want The Code To Run

    It would be helpful if you could attach a copy of your file including the buttons and explain in detail what you want to do referring to specific cells, rows, columns and sheets.

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,027

    Re: Button On Different Worksheet Than Where I Want The Code To Run

    @Richard Buttrey
    Hi Richard. Could I ask why you shouldn't use the sheet tab name? I always thought that using the tab name makes it much easier to identify which sheet is being referenced because most often the tab name is an indicator of the sheet contents or purpose. In a macro that has many sheet references I think that this could be an advantage.

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Button On Different Worksheet Than Where I Want The Code To Run

    Hard coding the sheet name in a macro puts you at the whims of a user who might change the tab name. A macro will fall over if this happens.

    It's always better to use the VB code name. You can if you wish change the VB CodeName to BE the same as the tab name if you wish.

    I suspect many of us don't bother to change the VB code name, I can't ever remember doing so after my early learning experience. In the VBE when you need to refer to the VB code name it's always visible, along with the tab name in the Project pane so no information is unavailable when you're coding.

    Not so bad if you are the only user and are aware of this potential hazard but I've lost track of the times in my early days before I realised the significance, of clients complaining that 'my workbook macro isn't working' only to establish that someone had changed the tab name
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  7. #7
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,027

    Re: Button On Different Worksheet Than Where I Want The Code To Run

    Thank you for your feedback, Richard. You have made an excellent point.

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Button On Different Worksheet Than Where I Want The Code To Run

    Yes. But a common mistake is to fail to fully identify and qualify any ranges you are wanting to use in your macro.
    If you start a macro with a button on say Sheet1 and the macro is intended to refer to cells on Sheer2, then code like

    Range("E1:J20").Copy
    will simply copy E1:J20 on sheet1. You should use

    Sheet2.Range("E1:J20").Copy
    And in VBA get into the habit of using the VBA Sheet code name NOT the sheet tab name, or worse the sheet index value (i.e. its position in the series of tab names)
    i.e. Sheet1. not Sheets("Sheet1").

+ 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] Copy ActiveX Button and Code to another Worksheet
    By Jan Lichtenbelt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-13-2014, 07:20 AM
  2. [SOLVED] VBA code that looks at which worksheet button was pushed
    By whatsmyname in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-08-2013, 02:39 PM
  3. [SOLVED] How can I change this worksheet code to a button hit?
    By JJBennett in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-28-2013, 11:43 AM
  4. Code for button errors due to worksheet name change
    By ge0rge in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-15-2008, 12:22 PM
  5. How do I code VBA to hit button on a different worksheet?
    By tbarker01@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-08-2006, 04:45 PM
  6. [SOLVED] c# code to add button control to the worksheet!!!!
    By .NetProf in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-30-2006, 12:45 AM

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