+ Reply to Thread
Results 1 to 25 of 25

Changing formulas to absolute references and, if not possible, converting them to mixed re

  1. #1
    Registered User
    Join Date
    09-10-2023
    Location
    Bras?lia, Brasil
    MS-Off Ver
    2010
    Posts
    8

    Changing formulas to absolute references and, if not possible, converting them to mixed re

    Hello everybody!
    The VBA code I have at the moment changes "L15" to "$L$15" in cells "A1" and "B1".
    I need ?L15? to have an absolute reference, also, in other cells (A2:B3), and when it is not possible, go for a mixed reference (e.g. in cell A2: ?=L15+ML15? for ?$L$15+ML$15?).

    Please Login or Register  to view this content.

    Please Login or Register  to view this content.
    Last edited by 6StringJazzer; 09-12-2023 at 08:40 AM. Reason: added code tags per guideline #2; formatted grid

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,440

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Welcome to the Forum Josecarlospdcj!

    First, your question is not clear because your code does not just change A1 and B1, it changes all occurrences of L15 to $L$15 in the whole range you have shown. Also I don't know what you mean by "when it is not possible". Are you trying to change L15 without changing things like ML15, because M$L$15 is illegal?

    Perhaps it would be clearer if you described your overall project and what you are trying to achieve.

    Also you do not need to activate objects to work on them, and unqualified objects will be assumed to be in ThisWorkbook. This code is equivalent to yours:
    Please Login or Register  to view this content.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,318

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Possibly...
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    09-10-2023
    Location
    Bras?lia, Brasil
    MS-Off Ver
    2010
    Posts
    8

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Thank you for the information about improvements to the VBA code.
    Sorry! I forgot to mention that there are formulas with more than 256 characters and that I have already tried some Excel add-ins.

    Trying to explain better.

    I have the following cells:
    A B C
    1 =L15+L16 =L15+L16 =M15+M16
    2 =L15+ML15 =L15+TL15 =M16+M17
    3 =L15+L16 =L15+M16 =M17+M18

    When activating the macro I get this result:
    A B C
    1 =$L$15+L16 =$L$15+L16 =M15+M16
    2 =L15+ML15 =L15+TL15 =M16+M17
    3 =L15+L16 =L15+M16 =M17+M18

    I need to achieve the following solution:
    L15 to $L$15 in cells: A1:B3
    L15 to L$15 in cells: A2 e B2

    A B C
    1 =$L$15+L16 =$L$15+L16 =M15+M16
    2 =$L$15+ML$15 =$L$15+TL$15 =M16+M17
    3 =$L$15+L16 =$L$15+M16 =M17+M18

    Thank you to everyone who is looking to help.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,440

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    I don't know why your original code only changes the first row. This is a brute-force approach that works:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    09-10-2023
    Location
    Bras?lia, Brasil
    MS-Off Ver
    2010
    Posts
    8

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Forgive me if the question is unreasonable.
    Could it be that the macro is stopping because it cannot make an absolute reference in the formula in cell A2, precisely in “ML15”, and cannot automatically jump to cell “B2” and following, where there is no such impediment?

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,440

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    That is very likely what it happening. I tested it with different formulas and it does work for all cells. This is not well documented by Microsoft.

  8. #8
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,550

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    For the formula =L15+ML15, the Replace command will change the formula to =$L$15+M$L$15.
    Artik

  9. #9
    Registered User
    Join Date
    09-10-2023
    Location
    Bras?lia, Brasil
    MS-Off Ver
    2010
    Posts
    8

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    A slip on my part.
    After I submitted my post, I saw your new code. So, I realized that the progress was enormous.
    Only the formulas for A2 and B2 were not changed.
    A2 to “=$L$15+ML$15”
    B2 to “=$L$15+TL$15”

    Can you continue helping me?

  10. #10
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,440

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Quote Originally Posted by Artik View Post
    For the formula =L15+ML15, the Replace command will change the formula to =$L$15+M$L$15.
    Artik
    It actually won't. M$L$15 is an illegal reference so it fails. Oddly the Replace method does not raise an error for creating an ill-formed formula (unlike other kinds of attempts to create an ill-formed formula). In the original code, the Replace command just stops when it hits an error and does not work on any other cells in the range.

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

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however it has been brought to our attention that the same query has been posted on one or more other forums and you have not told us about this. You are required to do so. Cross-posts are allowed but you must provide a link to your posts on other sites.

    Please see Forum Rule #7 about cross-posting and adjust accordingly. Read this to understand why we (and other sites like us) consider this to be important: https://excelguru.ca/a-message-to-forum-cross-posters/

    I am providing the link becuse you are a new member: https://www.mrexcel.com/board/thread.../#post-6104926

  12. #12
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,440

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Quote Originally Posted by Josecarlospdcj View Post
    Only the formulas for A2 and B2 were not changed.
    A2 to “=$L$15+ML$15”
    B2 to “=$L$15+TL$15”

    Can you continue helping me?
    There is nothing like this in the description of what you wanted. You are going to have to give a more detailed and rigorous specification of what you need.

  13. #13
    Registered User
    Join Date
    09-10-2023
    Location
    Bras?lia, Brasil
    MS-Off Ver
    2010
    Posts
    8

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Piggybacking on your code, I was able to get closer to what I needed.

    VBA code
    Please Login or Register  to view this content.
    With the first two lines of code I was able to change “=L15+ML15” to “=L$15+ML$15” and “=L15+TL15” to “=L$15+TL$15”

    In the third line of the code I tried, but "=L$15+ML$15" did not change to "=$L$15+ML$15" and “=L$15+TL$15” did not give the result “=$L$15+TL$15”

    I am quite hopeful.
    Last edited by 6StringJazzer; 09-16-2023 at 10:16 PM. Reason: added code tags per guideline #2

  14. #14
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2509 (Windows 11 Home 24H2 64-bit)
    Posts
    92,403

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Administrative Note:

    Please include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Guideline #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  15. #15
    Registered User
    Join Date
    09-10-2023
    Location
    Bras?lia, Brasil
    MS-Off Ver
    2010
    Posts
    8

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Thank you for the message.
    I will try to use these resources.

  16. #16
    Registered User
    Join Date
    09-10-2023
    Location
    Bras?lia, Brasil
    MS-Off Ver
    2010
    Posts
    8

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    As for the cross-posting, I clearly understood what I should have done and why I won't be a repeat offender.
    I sincerely apologize for what happened.

  17. #17
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,550

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    I seem to have found a solution. But it is not universal. It is prepared to solve only this problem.
    The idea of the macro is to go through the cells of the indicated range twice. In the first loop we swap only the longer addresses, and in the second loop we swap the shorter ones.
    Please Login or Register  to view this content.
    Artik

  18. #18
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,550

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Quote Originally Posted by 6StringJazzer View Post
    It actually won't. M$L$15 is an illegal reference so it fails. Oddly the Replace method does not raise an error for creating an ill-formed formula (unlike other kinds of attempts to create an ill-formed formula). In the original code, the Replace command just stops when it hits an error and does not work on any other cells in the range.
    Yes, I meant that there will be an attempt to replace the formula (which of course fails). But I am surprised by the performance of the Replace method. I never paid attention to this problem.
    Artik

  19. #19
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Josecarlospdcj,

    Select the cell(s) that you want to convert.
    Tested the code and it is working as you explained.
    Please Login or Register  to view this content.

  20. #20
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,440

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Quote Originally Posted by Artik View Post
    But I am surprised by the performance of the Replace method. I never paid attention to this problem.
    I never did either, until now. If you tried to make a direct assignment of that formula to the Formula property, it would raise a runtime error (1004 I think). But if Replace encounters an error, it is dangerously silent.

  21. #21
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,550

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    jindon, Ah, the power of RegEx.
    It seems that a few more screws need to be adjusted.
    1. for the formula:
    Please Login or Register  to view this content.
    does not change the second occurrence of L15
    2. oddly enough, for the formula
    Please Login or Register  to view this content.
    the address of ML15 is swapped twice
    3. for formula
    Please Login or Register  to view this content.
    does not change L15
    4. sometimes errors in the line
    Please Login or Register  to view this content.
    which I changed to
    Please Login or Register  to view this content.
    Artik
    Last edited by Artik; 09-16-2023 at 10:41 PM.

  22. #22
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    If OP asks in that way, I will change my code, but OP needs;

    Quote Originally Posted by Josecarlospdcj
    With the first two lines of code I was able to change “=L15+ML15” to “=L$15+ML$15” and “=L15+TL15” to “=L$15+TL$15”

    In the third line of the code I tried, but "=L$15+ML$15" did not change to "=$L$15+ML$15" and “=L$15+TL$15” did not give the result “=$L$15+TL$15”
    What else can anyone GUESS?

  23. #23
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,550

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    I'm afraid this is just the beginning. OP mentions:
    I forgot to mention that there are formulas with more than 256 characters
    Artik

  24. #24
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Quote Originally Posted by Josecarlospdcj View Post
    Sorry! I forgot to mention that there are formulas with more than 256 characters and that I have already tried
    Josecarlospdcj,

    e.g
    Convert
    =TT5*L5/(ML5+100)&"this is XL12"&XL5&"LL15"
    to
    =TT5*$L$5/(ML$5+100)&"this is XL12"&XL$5&"LL15"

    Select the cell(s) that you want to convert.

    Please Login or Register  to view this content.
    In case this doesn't work for any particular formula, I need to see them.

  25. #25
    Registered User
    Join Date
    09-10-2023
    Location
    Bras?lia, Brasil
    MS-Off Ver
    2010
    Posts
    8

    Re: Changing formulas to absolute references and, if not possible, converting them to mixe

    Artik, thank you immensely for your interest!

    I tested your code “Test_Art” and was able to observe the following:

    It works:
    Does not change cells with text, without formulas.
    Does not change empty cells.
    Only put dollar signs in formulas.
    It does not change what is written in quotation marks.
    Changes cells with less or more than 256 characters.

    It partially works:
    Changes "=L15+ML15" to "=L15+ML$15", including in the 1st cell with more than 256 characters.
    Changes "=L15+TL15" to "=L15+TL$15", including in the 1st cell with more than 256 characters.
    Changes only to mixed reference in lines. Even so, it only fixes the lines if "L15" is preceded by a letter other than "L" (example: Does not change "LL15" to LL$15).

    It does not work:
    Does not change "=L15+ML15" to "=$L$15+ML$15", even in cells with more than 256 characters.
    Does not change "=L15+TL15" to "=$L$15+TL$15", even in cells with more than 256 characters.
    Does not change "=L15+L16" to "=$L$15+L16", even in cells with more than 256 characters.
    Does not change "=L15+M16" to "=$L$15+M16", even in cells with more than 256 characters.
    Does not traverse all cells.
    Does not fix the formula columns.
    It gives "Run-time error "1004": Application definition or object definition error." in "For Each rngRef In Cell.Precedents"

+ 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. How to for converting absolute references to relative references in formulas
    By edspyhill01 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-11-2014, 02:09 AM
  2. [SOLVED] Can't figure this out for the life of me...Absolute, Relative, Mixed References
    By DallasTigah in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-11-2014, 01:20 AM
  3. [SOLVED] Macro: mixed absolute and relative references for rows
    By Jendill in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-12-2012, 08:12 PM
  4. Mixed references in VBA formulas
    By McNultyK in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-20-2011, 06:13 PM
  5. Replies: 0
    Last Post: 11-15-2007, 02:39 AM
  6. [SOLVED] Relative, absolute & mixed references in many cells
    By FTM in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 06-19-2006, 04:40 AM
  7. Converting to Absolute Cell References - en bloc
    By Basher Bates in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-11-2006, 05:10 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