+ Reply to Thread
Results 1 to 9 of 9

VBA Code to update last Max value if source cell has formula linking to another cell

  1. #1
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    VBA Code to update last Max value if source cell has formula linking to another cell

    Excel 2010 - VBA Code to update last Max value if source cell has formula linking to another cell

    Can someone please help?

    The following VBA code enters in cell C2 the max value ever entered in cell A2 which is a dynamic changing cell. In other words, cell A2 may have the value 2, then at another time it will the value 6, then 11... etc. The VBA code will write (for example) 11 in cell C2 if 11 was the last highest value used in cell A2.

    The code works fine as long as the dynamic value is entered manually in cell A2. However, if cell A2 has a formula pointing to another cell (in this example, pointing to cell A6) to retrieve the dynamic value then the VBA code will not update the max value in cell C2.

    How can the following VBA code be made to work with a formula in cell A2? NOTE: the code will work with the formula if I double click in the cell A2 and then press Enter. This will update the cell C2 correctly.

    Please Login or Register  to view this content.
    Please see attached file.

  2. #2
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: VBA Code to update last Max value if source cell has formula linking to another cell

    Why not trigger to A6 directly, instead of A2?

    PHP Code: 
    If Target.Address "$A$6" Then [C2] = WorksheetFunction.Max(Target, [C2]) 
    worksheet_change event does not trigger change from formula, but from manual input.
    Quang PT

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,467

    Re: VBA Code to update last Max value if source cell has formula linking to another cell

    A worksheet change event handler only fires if a change is made manually or by code, not as a result of a formula calculation.

    In this case, don't monitor cell A2, monitor cell A6 for changes. If the formula in cell A2 is more complex, change the WorksheetFunction.Max(Target, [C2]) to WorksheetFunction.Max([A2], [C2])
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: VBA Code to update last Max value if source cell has formula linking to another cell

    TMS, you guessed it right. Cell A2 has a bit more complex formula. Cell A6 is not actually a manual formula in my real workbook, it's an even more complex formula that merges several changing cells.

    Therefore, I need the code to grab the last highest number that passes through cell A2 without me having to manually update cell A2.

    I have also tried with Private Sub Worksheet_Calculate() to no avail. I guess I have to think of a different approach.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,467

    Re: VBA Code to update last Max value if source cell has formula linking to another cell

    Obviously, I don't know what your complex formula looks like, but this should work:

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: VBA Code to update last Max value if source cell has formula linking to another cell

    TMS,

    It works flawlessly.

    Thanks a billion.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,467

    Re: VBA Code to update last Max value if source cell has formula linking to another cell

    You're welcome. Thanks for the rep.

    My only concern about this sort of construct is that you have no audit trail. How do you track if you have missed one or more entries, or double entered them?

    Whatever, it meets the brief



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

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  8. #8
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: VBA Code to update last Max value if source cell has formula linking to another cell

    TMS,

    The worksheet uses VBA code that evaluates certain results and matches them to a grid while spiting out those values in some columns, and while it does that it also marks with an X the row being processed. Then there are some array formulas that check (doublecheck) that nothing was skipped, for example this one (which is in place in the workbook, but it would have been the formula in A2 in the example here provided): =MAX(IF($S$1:$S$3690="X",ROW($S$1:$S$3690))). This array formula spits out which row number is being processed. So, with your code, if we have 2,000 records it will tell me which record was the highest that was ever processed (based on the array formula) - this allows me to visually compare the total records to process vs the highest that was processed.

    Thanks for everything.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,467

    Re: VBA Code to update last Max value if source cell has formula linking to another cell

    You're welcome.

+ 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. Replies: 1
    Last Post: 02-18-2021, 09:32 AM
  2. VBA Code will not update as target cell is updated using a formula
    By neilcsmith in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-05-2020, 09:04 PM
  3. Replies: 2
    Last Post: 12-03-2014, 10:10 AM
  4. Replies: 2
    Last Post: 05-31-2013, 08:45 AM
  5. linking to source formula and not source value
    By coug4life86 in forum Excel General
    Replies: 1
    Last Post: 03-23-2011, 11:54 PM
  6. Linking cells: open source to update values
    By zimon72 in forum Excel General
    Replies: 2
    Last Post: 01-19-2006, 03:21 AM
  7. Replies: 0
    Last Post: 07-20-2005, 10:05 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