VBA: how to run a macro when certain variable cells change in Excel
Hello everybody.
I've been dealing with how to run a macro when certain cells change in Excel: I mean, under certain conditions a human being could manually change a value in a cell, and then, this change should trigger a recalculation in another cell. I'd like to precise that in my workbook, for internal reasons, it is no possible to use formulas.
Anyway, up to a certain point, the support Microsoft is clear.
Suppose I've a blank sheet with just a button to run a macro (see attachment "Recalculate-1").
Before running the macro "Update", i don't know what will be the result in terms of populated area.
Suppose, after the running, the new situation as in the second attachment ("Recalculate-2).
I need the green cells triggering recalculation, identified as follows:
- for each value "6" on column B, the green cells have to be - in the same row - from column D to the last populated column in the sheet (in this case, F);
- in event of inserting manually a new value in the green cells, recalculate the sum in column C of the same row (so, suppose I change manually the value in cell E19 from 150 to 100, then the new value in C19 will be 400).
Yes, it works, but very slowly.
I mean: when the macro "Update" is called via the button, it takes several minutes to end the task.
Probably I've to stop some sort of loops created by the process Worksheet_Change.
Subodh Tiwari (Neeraj)
In the code underneath the Update button, you may disable to events so that the change event code will not be triggered when the Update code tries to write the data onto the Sheet.
To do that, in your Update code, add the following line in the beginning of the code...
If in your Update code, you are using Exit Sub statement anywhere in the code, please remember to enable the events by adding the above line of code before the Exit Sub line.
for each of the cells included in the ranges, if the cell change value then the new value of the underneath cell (maybe something like cell.offset(1, 0).value ) is cell.Offset(-1, 0).Value / cell.Value
Subodh Tiwari (Neeraj)
The last step should be: once recalculated
Cells(Target.Row, 3) = Application.Sum(rng)
In which code? Do you mean in the Update code?
In that case you need to tweak your Update code to display the correct sum in column C. Isn't it doing that in the existing Update code?
for each of the cells included in the ranges, if the cell change value then the new value of the underneath cell (maybe something like cell.offset(1, 0).value ) is cell.Offset(-1, 0).Value / cell.Value
I am not sure how the values are populated on the Sheet by the Update code. The proposed solution is based on the screenshot of the data you shared and it will produce the desired output once any value gets changed manually.
For your new calculations in the existing code, you may open a New Question by providing the enough description along with a sample file if required.
Paolo Crossi
ASKER
Ok, no problem, I figured it out exploiting the ideas showed in your previous posts.
Thank you.
Excel work much better with formulas than with VBA.