+ Reply to Thread
Results 1 to 8 of 8

Re-using same macro applied to different Sheets in same Workbook

  1. #1
    Registered User
    Join Date
    08-12-2014
    Location
    Athens, GA
    MS-Off Ver
    10
    Posts
    4

    Re-using same macro applied to different Sheets in same Workbook

    I have three sets of tabs (sheets) in the same workbook. Each of these sets of three sheets work precisely the same way, except they have different data sources. I have a macro the performs data manipulations for one set of sheets. I want to use that same macro against each of the other two sets of sheets. I do not want to copy the macro two times then "replace" the sheet names. That will work, but then I must maintain three macros. Example: I want to run the macro with "IRB" as the sheet to be selected, rather than "UserMgt":

    Sheets("UserMgt").Select

    Is there a way to change "UserMgt" to a variable that can be changed each time the macro is called?

  2. #2
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,189

    Re: Re-using same macro applied to different Sheets in same Workbook

    use ACTIVESHEET instead of hardcoded names like usermgt.
    (and dont use activesheet.select, because you are already there)

  3. #3
    Registered User
    Join Date
    08-12-2014
    Location
    Athens, GA
    MS-Off Ver
    10
    Posts
    4

    Re: Re-using same macro applied to different Sheets in same Workbook

    I reckon I was not clear about my goal. I am not "already there". The macro is moving and manipulating data from one sheet to another (three sheets involved). So I kickoff the macro with a macro button. I want to tell the macro when I click the "UserMgt Update" button that it should use the three sheets beginning with "UserMgt". When I click the "IRB Update" button, the macro should use the three sheets beginning with "IRB".

  4. #4
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,189

    Re: Re-using same macro applied to different Sheets in same Workbook

    Then always select the workbook you want to be IN:

    sheets("UserMgt").select
    'do stuff here

    sheets("IRB").select
    sheets("UserMgt").range("A1")=true

    'do stuff here

  5. #5
    Registered User
    Join Date
    08-12-2014
    Location
    Athens, GA
    MS-Off Ver
    10
    Posts
    4

    Re: Re-using same macro applied to different Sheets in same Workbook

    That is what I do now (sheets().select). So I have identical macro code with only the sheets selected as different statements. So I have 4 copies of the same macro which each marco selects 6 worksheets. So there are 6x4 worksheets. Using your example, it looks like this:

    One macro says:
    sheets("UserMgtData").select
    'do stuff here

    Another says:
    sheets("IRBData").select
    'do exactly the same stuff here
    ----------------------------
    I want one macro where I can prefix the sheets name:
    sheets("prefixDATA).select

    then somehow pass that prefix to the macro, which may be "Grants" or "IRB" so that the macro makes that prefix substitution on the sheets().select statement.

    Sorry for being so unclear, and I appreciate your help.

  6. #6
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Re-using same macro applied to different Sheets in same Workbook

    May be something like this.....

    Assuming the caption of your command button is "Update UserMgt" or "Update IRB".
    Please Login or Register  to view this content.
    Does this help?
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  7. #7
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Re-using same macro applied to different Sheets in same Workbook

    The sample sheet is attached here.

    Code:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    08-12-2014
    Location
    Athens, GA
    MS-Off Ver
    10
    Posts
    4

    Re: Re-using same macro applied to different Sheets in same Workbook

    Thank you, sktneer. Let me work with your suggestion. I believe you are showing me what I need to know. I'll get back to you.

+ 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. No More New Fonts May Be Applied to this Workbook
    By docmaint in forum Excel General
    Replies: 2
    Last Post: 01-02-2009, 02:22 PM
  2. No more new fonts may be applied in this workbook!!!
    By WhytheQ in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-04-2006, 12:50 AM
  3. Replies: 1
    Last Post: 08-02-2006, 01:45 PM
  4. Cancel a macro already applied to all sheets
    By minrufeng in forum Excel General
    Replies: 5
    Last Post: 02-21-2006, 07:25 PM
  5. Generic Page Setup Macro applied to multiple workbooks with varying number of sheets
    By sowetoddid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-22-2005, 11:45 AM

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