+ Reply to Thread
Results 1 to 15 of 15

Modify formulas in range based on cell value

  1. #1
    Registered User
    Join Date
    10-12-2010
    Location
    cali
    MS-Off Ver
    2010
    Posts
    15

    Modify formulas in range based on cell value

    I have two sheets with formulas like the following.

    =IF(Slct_Report=1,HWF!B12,AWF!B21)
    =IF(Slct_Report=1,HWF!B9,AWF!B10)

    I'm trying to write a macro to remove the 'if' statement so that I get:

    =HWF!B12

    or

    =AWF!B10

    based on the value of Slct_Report

    Thanks very much for your help...

  2. #2
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Modify formulas in range based on cell value

    Hello podunkus,

    Your IF formula implies that you have a Named Range which contains, or refers to only one Cell.

    Could you please explain why you want a macro to remove the "if", or even better, upload a sample Workbook showing us what you have, and how you would like to have it.

    Regards
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,691

    Re: Modify formulas in range based on cell value

    Install this code in a Module, and call it for each of the two worksheets you want it to work on.

    This assumes that the named range will contain and be compared to a numerical value in all formulas. I only tested for your examples but made it general enough so that it should work for any named range, and any expressions for the TRUE and FALSE parts.

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Registered User
    Join Date
    10-12-2010
    Location
    cali
    MS-Off Ver
    2010
    Posts
    15

    Re: Modify formulas in range based on cell value

    I'm removing the 'IF' statement because the sheets are being copied to a new workbook. The new workbook will contain either the sheet named HWF or AWF plus the two sheets (Summary1 and Summary2) I mentioned that contain the IF statements. So the process so far is to copy the three sheets to the new workbook, then modify the IF statement to remove the reference to the sheet that does not exist. I'm open to a different design suggestion.

  5. #5
    Registered User
    Join Date
    10-12-2010
    Location
    cali
    MS-Off Ver
    2010
    Posts
    15

    Re: Modify formulas in range based on cell value

    Quote Originally Posted by 6StringJazzer View Post
    Install this code in a Module, and call it for each of the two worksheets you want it to work on.

    This assumes that the named range will contain and be compared to a numerical value in all formulas. I only tested for your examples but made it general enough so that it should work for any named range, and any expressions for the TRUE and FALSE parts.
    Jeff this looks interesting but I couldnt get the code to execute. Each time I hit run or step into I was prompted to select a macro to run.

    Thanks!!

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,691

    Re: Modify formulas in range based on cell value

    This macro requires a single argument, which is the worksheet containing the formulas you want to change. You cannot run a Sub with arguments by running it by itself, it has to be called from somewhere else to provide the arguments.

    If this is a one-shot deal, I suggest you try this:

    1. With your file open in Excel, hit ALT-F11 to open the VBA development window
    2. Hit CTRL-G to open the Immediate window
    3. In the first line of the Immediate window type:

    Please Login or Register  to view this content.
    where Sheet1 is the actual name of the worksheet containing the formulas you want to modify. Then hit Enter and the macro will run for that worksheet.

  7. #7
    Registered User
    Join Date
    10-12-2010
    Location
    cali
    MS-Off Ver
    2010
    Posts
    15

    Re: Modify formulas in range based on cell value

    Quote Originally Posted by 6StringJazzer View Post
    This macro requires a single argument, which is the worksheet containing the formulas you want to change. You cannot run a Sub with arguments by running it by itself, it has to be called from somewhere else to provide the arguments.

    If this is a one-shot deal, I suggest you try this:

    1. With your file open in Excel, hit ALT-F11 to open the VBA development window
    2. Hit CTRL-G to open the Immediate window
    3. In the first line of the Immediate window type:

    Please Login or Register  to view this content.
    where Sheet1 is the actual name of the worksheet containing the formulas you want to modify. Then hit Enter and the macro will run for that worksheet.
    When I try this I get an invalid procedure call or argument


    on line:

    RangeName = Mid(Arr(0), 5, InStr(2, Arr(0), "=") - 5)

  8. #8
    Registered User
    Join Date
    10-12-2010
    Location
    cali
    MS-Off Ver
    2010
    Posts
    15

    Re: Modify formulas in range based on cell value

    Here is the actual file I'm working on. Thanks very much!
    Attached Files Attached Files

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,691

    Re: Modify formulas in range based on cell value

    I took your file, unchanged, and typed the following commands into the Immediate window. There were no errors and it worked fine.

    Please Login or Register  to view this content.
    If you use this Sub on worksheets that contain IF formulas other than the type that you described above then that might cause the error you saw. This Sub is very specific to the type of formula you mentioned and I did not write it to be robust enough to ignore other IF formulas.

  10. #10
    Registered User
    Join Date
    10-12-2010
    Location
    cali
    MS-Off Ver
    2010
    Posts
    15

    Re: Modify formulas in range based on cell value

    Quote Originally Posted by 6StringJazzer View Post
    I took your file, unchanged, and typed the following commands into the Immediate window. There were no errors and it worked fine.
    Yes I got it working! Do you think there is a way to get it to perform on the newly created workbook? If you can take another look, when I call ResolveFormulas from the Sub CopytoNewWB() macro, (the call is currently commented out) I get the compile error. I think its because Slct_Rport doesnt reside in the new workbook.

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,691

    Re: Modify formulas in range based on cell value

    Quote Originally Posted by podunkus View Post
    ...I get the compile error. I think its because Slct_Rport doesnt reside in the new workbook.
    First, let me emphasize that the code looks for a formula of this format:

    =IF(<range expression>=<numeric constant>,<expression>,<expression>)

    If the formula doesn't conform to this syntax, the code will give unpredictable results, including runtime errors.

    If there is no range called Slct_Rport what do your formulas look like? If you use a named range in formulas that does not exist, it will cause a formula error on the worksheet. It will also cause the code to fail--it will not give you a compile error, it will give you a runtime error. Whenever asking for help on an error, please give the entire error message, and the line of code that causes it.

  12. #12
    Registered User
    Join Date
    10-12-2010
    Location
    cali
    MS-Off Ver
    2010
    Posts
    15

    Re: Modify formulas in range based on cell value

    Quote Originally Posted by 6StringJazzer View Post
    First, let me emphasize that the code looks for a formula of this format:

    =IF(<range expression>=<numeric constant>,<expression>,<expression>)

    If the formula doesn't conform to this syntax, the code will give unpredictable results, including runtime errors.

    If there is no range called Slct_Rport what do your formulas look like? If you use a named range in formulas that does not exist, it will cause a formula error on the worksheet. It will also cause the code to fail--it will not give you a compile error, it will give you a runtime error. Whenever asking for help on an error, please give the entire error message, and the line of code that causes it.
    The resolvefomulas macro you created for me is being called from within Sub CopytoNewWB()
    CopytoNewWB creates a new workbook by copying a subset of sheets from the existing workbook.
    At the end, resolveformulas is called to fix the formulas in the new workbook.

    In the original workbook the formulas look like this:

    =IF(Slct_Report=1,HWF!C4,AWF!C3)

    In the newly created workbook, the formulas look like this:

    =IF(Slct_Report=1,[CopyandFormulaUpdate.xlsm]HWF!C4,[CopyandFormulaUpdate.xlsm]AWF!C3)

    Your macro works perfectly if executed on the Summary! page in the existing workbook. I'm trying to get it to work on the Summary! page that is in the newly created workbook.

    The named range Slct_Report does not exist in the newly created workbook.

    When I try to run the VBA and call resolveformulas I get 'Compile error: Argument not optional'
    the line of code highlighted is 'Call resolveformulas'

    Thanks!
    Last edited by podunkus; 11-01-2013 at 07:31 PM.

  13. #13
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,691

    Re: Modify formulas in range based on cell value

    ResolveFormulas requires a worksheet as an argument; the error message is telling you that the argument is missing. One way to specify the worksheet is

    Please Login or Register  to view this content.
    You must be using it correctly when calling from CopytoNewWB; you just need to do the same thing here.

  14. #14
    Registered User
    Join Date
    10-12-2010
    Location
    cali
    MS-Off Ver
    2010
    Posts
    15

    Re: Modify formulas in range based on cell value

    Quote Originally Posted by 6StringJazzer View Post
    I took your file, unchanged, and typed the following commands into the Immediate window. There were no errors and it worked fine.

    Please Login or Register  to view this content.
    If you use this Sub on worksheets that contain IF formulas other than the type that you described above then that might cause the error you saw. This Sub is very specific to the type of formula you mentioned and I did not write it to be robust enough to ignore other IF formulas.
    Thanks very much for all your help. This code works perfectly as long as there are no other formulas on the worksheet. I would like to add some non "IF" formulas to the page but I dont know if thats possible.

    I wonder if we modified this line:

    Please Login or Register  to view this content.
    to:

    Please Login or Register  to view this content.
    maybe it would ignore all other formulas.

    EDIT: this change works partially. It converted several formulas on the page but then stopped with an application-definded or object-defined error on:

    Please Login or Register  to view this content.
    Last edited by podunkus; 11-04-2013 at 02:00 AM.

  15. #15
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,691

    Re: Modify formulas in range based on cell value

    Sorry for the long delay, it's been a very busy month. What were the values of C.Formula and FalseExp prior to reaching this statement?

+ 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] Got range of a cell, now modify that to be range of entire column
    By ShawnW in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-19-2013, 11:46 AM
  2. Modify formulas on the entire sheet based on dropdown selection
    By robolist in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-06-2013, 07:22 AM
  3. [SOLVED] How do I call F2 in VBA and use a macro to modify formulas in a cell?
    By gsjan1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-16-2012, 04:19 PM
  4. navigating while writing cell formulas - any way to modify default functionality?
    By riwiseuse in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-20-2011, 08:32 AM
  5. Modify the # of cells included in a range based on formula
    By frenkile in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-25-2011, 09:16 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