+ Reply to Thread
Results 1 to 8 of 8

Insert a value in a cell based on what macro is run

  1. #1
    Registered User
    Join Date
    07-21-2014
    Location
    Toronto, ON
    MS-Off Ver
    MS Office 2010
    Posts
    55

    Insert a value in a cell based on what macro is run

    Hi. I have a spreadsheet where I added macros (attached to buttons) that filter information on a long report based on values in 1 column (the information is a department name). There are 7 different macros for the 7 departments. The code I used is
    Please Login or Register  to view this content.
    The Department is in the array ("D&C" in this instance. "PN" will appear in all the macros) and I named the Macro "Sub DC_Click()". All the other macros follow the same format with the department name changing. What I want to do is insert the Department name in a cell in the Header of the report based on what macro is running. So if the user selects this macro for "D&C", "D&C" will appear in cell E2 (not the "PN"). How would I do this? The code will need to say something like if Sub DC_Click() is run, insert "D&C" in cell E2; if Sub Legal_Click() is run, insert "Legal" in cell E2, etc., continuing for all 7 departments. Thank you.

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Insert a value in a cell based on what macro is run

    Change your subroutine to accept a "Department" parameter, then pass a specific value from each button click - something like:
    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    07-21-2014
    Location
    Toronto, ON
    MS-Off Ver
    MS Office 2010
    Posts
    55

    Re: Insert a value in a cell based on what macro is run

    Thanks Olly. This worked exactly as I asked. Wonderful! I do have another follow up question: I have abbreviated some of the department names that I have in column "V" due to the column width. For example, "Dev Analysis" instead of "Development Analysis". Is there a way to change the code so I can have the full name not the abbreviation? I tried changing "foo" to the full name in the sub but that obviously doesnt work since that is not the value in the filter. Any suggestions? thanks

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Insert a value in a cell based on what macro is run

    You just need some sort of lookup, to map the abbreviated values to the full values. You could store these in a table on a worksheet, or in your code. Which approach you take is up to you, and probably depends on who and how you need to maintain this mapping.

  5. #5
    Registered User
    Join Date
    07-21-2014
    Location
    Toronto, ON
    MS-Off Ver
    MS Office 2010
    Posts
    55

    Re: Insert a value in a cell based on what macro is run

    Ok. I will try that. Thank you again. Enjoy your weekend.

  6. #6
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Insert a value in a cell based on what macro is run

    A quick addition: one method of managing abbreviated and full department names, in code:

    Please Login or Register  to view this content.
    Maintain the arrays of Abbreviated and matching Full department names, then column V will be filtered based on the abbreviation, and cell E2 wil display the full name,


    If you prefer a different method for mapping abbreviated values to full values, then the code highlighted blue is the bit you'll need to change.


  7. #7
    Registered User
    Join Date
    07-21-2014
    Location
    Toronto, ON
    MS-Off Ver
    MS Office 2010
    Posts
    55

    Re: Insert a value in a cell based on what macro is run

    OMG! Perfect! I was just sitting here debating about a lookup table and investigating another code method when your message came through! Perfect! Thanks so much for the assistance. I owe you a virtual beer!

  8. #8
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Insert a value in a cell based on what macro is run

    Happy to help Thanks for the feedback!

+ 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] Excel Macro, VBA help needed - Insert Cell based on value of a cell in another column
    By alw99 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-03-2014, 05:22 PM
  2. [SOLVED] Event Macro to insert text in cell based on input of another cell
    By nplouffe in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-08-2012, 11:54 AM
  3. [SOLVED] VB Macro to insert value in cell based on multiple criteria in a row
    By haleakala17 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-23-2012, 10:30 PM
  4. [SOLVED] Macro to insert lines based on cell content
    By MirTraynor in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-08-2012, 11:06 AM
  5. Need help with insert row macro based on cell criteria
    By RAK in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-30-2009, 12:18 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