+ Reply to Thread
Results 1 to 14 of 14

Macro to change formula

  1. #1
    Registered User
    Join Date
    08-30-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    78

    Macro to change formula

    Hi all,

    I'm trying to produce what I think should be a reasonably simple macro.

    I have multiple business units in the business and, based on which business unit is selected the formulas should then consolidate data from other sheets into a final presentation.

    For example; two business units are 282 and 302. If a user selects business unit 282 then I want the macro to then go off to several different sheets. In those sheets it should replace all formula references to any other business unit (lets say it's 302) and replace those references with a reference to business unit 282.

    In order to make sure the macro runs correctly next time the final part of the macro is to store the newly selected business unit, hard coded into another cell. My plan was that the user selects a new business unit from a drop down list and then runs the macro. When the macro is run the first command is that this cell (with the previously copied business unit hard coded) would be selected, the value copied and, using the find all command, pasted into the "find" field. Then the drop down cell would be selected and whatever business unit the user has picked, this would be copied and pasted into the "Replace" field.

    Then three sheets would be selected, one at a time, the columns highlighted and the "Replace All" command would be executed.

    Once this has been done the newly picked business unit would be pasted special, value only, into another cell. Then the macro ends. When the macro is run again it's first command is to go to this cell and copy the value to paste into the "find" field. Thus the macro should update and whether the user picks business unit 339 or 612 next time should make no difference.


    My problem is demonstrated in the excerpt of the macro that I've pasted below. The business units 282 and 302 are coded into the macro and so the macro will only run for these business units.

    How do I get the macro to be dynamic enough to pick up whatever the new business unit has been selected?


    Excerpt of macro:

    Please Login or Register  to view this content.
    End macro excerpt
    Last edited by Wizards; 11-23-2017 at 06:36 PM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: Macro to change formula

    Please edit your post and add code tags to the code.




    AA CodeTags.jpg

  3. #3
    Registered User
    Join Date
    08-30-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: Macro to change formula

    Excerpt of macro:

    Please Login or Register  to view this content.
    End macro excerpt

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to change formula

    Hi Wizards,

    We were expecting you to edit your original post with the code tags
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  5. #5
    Registered User
    Join Date
    08-30-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: Macro to change formula

    How's that?

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to change formula

    Excellent

  7. #7
    Registered User
    Join Date
    08-30-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: Macro to change formula

    Any additional help would be greatly appreciated.

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to change formula

    Does this do the job?

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    08-30-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: Macro to change formula

    Hi Xl,

    I don't understand all that you've done in that post.

    Are you defining worksheets being referenced; ie. wi means the "Income" worksheet and R means Range?

    OldVal is found in cell A2 and NewVal is found in cell c11. Not sure why cell A2 would be referenced.


    Ok - I changed that to OldVal = Cells(1, 1) and I get the first part of the Macro to work. Thank you. Unfortunately the second part doesn't work but I might be able to work that out on my own with this information.

    Thanks for your help.

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to change formula

    OldVal is found in cell A2 and NewVal is found in cell c11. Not sure why cell A2 would be referenced.
    I'm confused. A2 is Row 2 Column 1 or Cells(2,1) Is there another sheet involved i.e. Three Sheets??

  11. #11
    Registered User
    Join Date
    08-30-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: Macro to change formula

    No other sheet - just that the old value that I was trying to replace was found in cell reference A1.

    When I used the statement
    Please Login or Register  to view this content.
    the Macro wasn't doing anything. However, on changing that to
    Please Login or Register  to view this content.
    the first part of the macro is working.


    If I want to define two or more values to replace can I just use names like OldVal1 and OldVal2?

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to change formula

    Sure - but do you mean that no replacement is occurring - could it be the apostrophe?

  13. #13
    Registered User
    Join Date
    08-30-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: Macro to change formula

    Yeah - there was no replacement occurring. But as soon as I changed it to Cells(1,1) the replacement was happening beautifully.


    Thanks again.

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to change formula

    You're welcome!

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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 change formula
    By madsklavsen in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 07-19-2016, 04:53 PM
  2. [SOLVED] Change Macro Formula
    By boldcode in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-01-2014, 03:35 PM
  3. Macro to change formula to value
    By graiggoriz in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-28-2014, 12:39 PM
  4. Got a formula I would like to change into a macro.
    By tripey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-22-2012, 10:49 AM
  5. Macro to change formula
    By yogesh thakker in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-15-2010, 10:35 AM
  6. Macro to Change Formula
    By floridagunner in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2007, 09:59 AM
  7. Change Formula with Macro
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2006, 02:25 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