Avatar of Paolo Crossi
Paolo Crossi
 asked on

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.

https://support.microsoft.com/en-us/help/213612/how-to-run-a-macro-when-certain-cells-change-in-excel

In particular, if I know in advance the range that has to cause a recalculation, no problem:

Set KeyCells = Range("A1:C10")

Open in new window


Now, my task is harder.

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).

Could anyone help me?
recalculate-1.bmp
recalculate-2.bmp
VBAMicrosoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Subodh Tiwari (Neeraj)

8/22/2022 - Mon
Fabrice Lambert

Why not just a formula ?

Excel work much better with formulas than with VBA.
ASKER CERTIFIED SOLUTION
Subodh Tiwari (Neeraj)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Paolo Crossi

ASKER
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...
Application.EnableEvents = False

Open in new window

And then enable the events before the Update code ends i.e. add the following line of code before the End Sub line in your Update code.
Application.EnableEvents = True

Open in new window


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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Paolo Crossi

ASKER
Yes, well, now it takes few seconds.

The last step should be: once recalculated

Cells(Target.Row, 3) = Application.Sum(rng)

 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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Subodh Tiwari (Neeraj)

You're welcome Paolo!