+ Reply to Thread
Results 1 to 14 of 14

VBA not updating when cell values are changed

  1. #1
    Registered User
    Join Date
    07-14-2021
    Location
    Brighton,
    MS-Off Ver
    2021 365
    Posts
    33

    VBA not updating when cell values are changed

    Hi there,

    I am very new to VBA and I don't have much knowledge, so please bear with me.

    I am making a financial budget workbook in Excel and I am trying to see how much I can save each month. I have cells in Excel that contain a certain amount. These amounts have to be added of subtracted from my initial saving from the previous month. The cells that contain the amounts are in F5 and F19. However, when I change the value of these cells it does not update to the total amount.

    Here is my short script from VBA:

    Please Login or Register  to view this content.
    So basically, when I change the values in cells F5 and F19 in Excel, the total value does not change of c2 in cell H9. I am confused as to why this does not update automatically. Could some please help?

    Best Wishes!

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA not updating when cell values are changed

    That's the basic procedure - you now need a way to run it...

    Open the VBA editor and double click the Sheet in the project tree to open the code window. In the dropdown on the right hand side above the code window select 'Change'. You should then see a procedure stub like
    Please Login or Register  to view this content.
    This procedure will run whenever that sheet is changed so you can call your other procedure, or copy it into this procedure... your call.

    This is only the start, you also need to check if either of cells F5 and F19 were edited before calling the procedure, but trying adding a bare message box into the blank procedure above and check you can see the message when you edit any cell
    Last edited by cytop; 05-10-2022 at 09:36 AM.

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

    Re: VBA not updating when cell values are changed

    This sub will run only if you explicitly call it. It will not happen automatically.

    If you want something to happen automatically when a cell is changed, you need to use the Change event. In the module for the worksheet that contains this data, use this code (red indicates what I changed from your original code). Also you have undeclared variables. I strongly recommend to everyone that they use Option Explicit and declare variables. Doing so prevents a lot of bugs and runtime errors.

    Also I recommend indentation to show the control structure.

    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
    07-14-2021
    Location
    Brighton,
    MS-Off Ver
    2021 365
    Posts
    33

    Re: VBA not updating when cell values are changed

    Hi Cytop, thank you for your help.

    However, this happens:

    screen.jpg
    Last edited by 6StringJazzer; 05-10-2022 at 10:40 AM. Reason: Fixed image attachment

  5. #5
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA not updating when cell values are changed

    Can't see- get an invalid attachment message.

    Try what 6String... posted above. It's essentially the same as what I suggested but also includes your code (And no, I've not tested it either).

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA not updating when cell values are changed

    No Variable declarations...
    Can see this going pear-shaped...I suggest uploading a sample file and explaining exactly what it is you are wanting to achieve...
    See top yellow banner
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

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

    Re: VBA not updating when cell values are changed

    Quote Originally Posted by cytop View Post
    Can't see- get an invalid attachment message.
    I fixed it.

  8. #8
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA not updating when cell values are changed

    You have 2 procedure declarations and 2 'End Sub' statements... Delete the 'Sub Monthly_Savings' line and 1 of the 'End Sub' lines.

    Also, that code is in the Selection_Change event which means it will run every time you move to a different cell. You need it in the Worksheet_Change event.

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

    Re: VBA not updating when cell values are changed

    Please replace your code with what is in post #3 and try that.

  10. #10
    Registered User
    Join Date
    07-14-2021
    Location
    Brighton,
    MS-Off Ver
    2021 365
    Posts
    33

    Re: VBA not updating when cell values are changed

    Thanks Guys for your help.

    6StringJazzer, I tried your code and it still isn't working. Sorry.

    So as Sintek requested I have uploaded a sample file.

    So to reiterate again, I want cell H9 to automatically update if the value in F5 and F19 changes. F19 is controlled by the amounts stored in cells B9 to B18. These cells are my expenses. So if I want to increase the amount of say H, F19 updates, but cell H9 does not.

    Cells F5 and F19 are in the VBA script. In Excel cell F19 changes value if I change an amount in any of the B cells, but H9 does not change.

    Many thanks

    PS, perhaps you could explain what you mean by declaring variables? I always thought if I put say 'c = 20', that I am declaring a variable.
    Attached Files Attached Files

  11. #11
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA not updating when cell values are changed

    More bad news ... F5 & F19 contain formulas. Formulas do not fire the WorkSheet change event when they recalculate so you need to step back and think again.

    I'm sorry, I can't follow the logic of what you have in that workbook but as a first step perhaps look for changes in B3:B5 & B9:B18 - but even those shouldn't be hard-coded. What if you added another row in each block?

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

    Re: VBA not updating when cell values are changed

    By the way, the code in your file is not the same as what you posted.

    F5 and F19 are formulas. The Change event does not occur when the result of a formula changes, only if the actual content of a cell changes.

    Also, F5 and F19 are sums of the rows above. It looks like you plan on inserting new rows, so those totals will not be in F5 or F19 after you insert a new row; they'll be in F6 or F20, for example. You have the same issue with H9.

    One way to do this is to make these totals named ranges, and keep a copy of their last values on another sheet. Then monitor the Calculate event, and compare the values every time there is a calculation on the sheet. If that values don't match, make your changes then update the "last value". I'm not 100% sure that will work for you because I don't know how you plan to use this. But I have made those changes in your file.

    Also, I don't understand your big picture here, but you seem to be hard-coding some dates in your VBA. It appears that you will have to update your code every month. There's a better way to do that but I don't know what you're doing so I can't offer a solution just yet. What kind of updates will you be making to this file and when?

    What you are doing is assigning a value to a variable. Declaring a variable is using the Dim statement to create the variable and indicate its data type. If you do not do this, then VBA will assume that every variable that is not declared is a Variant type. This can cause a lot of bugs if you misspell a variable name in your code. I strongly recommend to everyone that they use Option Explicit and declare variables. Doing so prevents a lot of bugs and runtime errors.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    07-14-2021
    Location
    Brighton,
    MS-Off Ver
    2021 365
    Posts
    33

    Re: VBA not updating when cell values are changed

    Hi Guys, sorry I have been really dumb. The reason it won't change value is because I have coded it to update at the end of each month which I did intend. I have gone through the loop using the F8 keys and seen that it does indeed update and takes into account the amounts in cells F5 and F19.

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

    Re: VBA not updating when cell values are changed

    If your question has been answered please mark your thread as "Solved" so that members will know by looking at the thread title that your problem is solved. Go to the menu immediately above your first post to the thread and click on Thread Tools. From the dropdown menu select "Mark this thread as solved..."

    If a member helped you solve your problem, consider adding to their reputation by clicking on the star icon addreputationiconsmall.jpg below their name.

    If you solved your own problem, please provide your solution so that somebody seeing this thread in a search for the same problem will see how it was solved.

+ 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. Dropdown Cell not updating if source is changed
    By belaprops in forum Excel General
    Replies: 2
    Last Post: 01-14-2022, 01:11 AM
  2. [SOLVED] Updating cell values after an item in combobox has changed?
    By Sybille in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-20-2019, 10:41 AM
  3. [SOLVED] cells linked not updating when original cell is changed
    By chriswrcg in forum Excel General
    Replies: 1
    Last Post: 09-19-2018, 09:06 AM
  4. [SOLVED] Date that cell changed to each of 3 values
    By leovfx in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-21-2017, 01:10 PM
  5. [SOLVED] Write Current Time Next To Changed Cell (without updating it)
    By namialus in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-09-2014, 06:59 AM
  6. Determining which cell changed values
    By mwc0914 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-10-2012, 10:21 AM
  7. Chart not updating when data changed
    By Thomas-W in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 07-20-2011, 01:36 PM

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