Macro to increment a cell value based on two conditions being met

RPM7

Board Regular
Joined
Nov 28, 2007
Messages
191
Does anyone know if its possible to run a macro that checks if certain conditions are met and if they are not, it incrementally increases another cell.

Condition 1: W15>$W$12
Condition 2: X15<=$X$12

If the above two conditions are not met, then increase the cell value of AD15 by 1 otherwise, ignore and proceed with the next row.
This would need to be repeated for rows 16 to 44.

This is to be applied to a number of worksheets and would really make my life easier, if there was an automated way of doing it.
If anyone can provide any advice, I'd be very grateful.

Cheers
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
We need more information about :

This is to be applied to a number of worksheets
Will each sheet have a Range ("AD15") you want to advance

And we need to know what sheets.
Do you want this script to run on all sheets at once or do you plan to have a button on each sheet and will run the script when you want.
 
Upvote 0
This script will run on one sheet.
Do not know what you mean when you say numerous sheets.
We would need sheet names.

Code:
Sub Test()
Application.ScreenUpdating = False
Dim i As Long
    For i = 15 To 44
       If Cells(i, "W").Value > Range("W12").Value And Cells(i, "X").Value <= Range("X12").Value Then
       
       Range("AD15").Value = Range("AD15").Value + 1
       
       End If
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for the reply 'My Answer Is This'.

I think the macro needs a little tweak though.
I'm not sure if I've read the code right, but I think it only modifys AD15.

I don't know how to explain it properly but I want it to loop through AD15:AD44 whilst running the two conditions for each respective row.
Pushing the button once will check and update each row until the 2 conditions are met.

i.e. if the cell AD15 needed to be 4, I would need to check the conditions 5 times and then move to the next row.

1st pass: AD15=0, IF ( OR ( W15<$W$12 , X15>$X$12 ) , AD15 value + 1,
2nd pass: AD15=1, IF ( OR ( W15<$W$12 , X15>$X$12 ) , AD15 value + 1,
3rd pass: AD15=2, IF ( OR ( W15<$W$12 , X15>$X$12 ) , AD15 value + 1,
4th pass: AD15=3, IF ( OR ( W15<$W$12 , X15>$X$12 ) , AD15 value + 1,
5th pass: AD15=4, Continue to next row (AD16) and repeat the same process until AD44 meets the conditions for W44 & X44.

I was trying to keep it somewhat simple.
My intention is to just create a button to populate the entire range AD15:AD44 in one go.
At the moment I'm manually updating the the cell AD for each row and using a conditional format to flag if the 2 conditions aren't met.

With regards to the other worksheets, I was going to edit the macro and create a new button for each worksheet and adjust the cell reference as required.

I hope that makes sense.
 
Upvote 0
You said:
i.e. if the cell AD15 needed to be 4, I would need to check the conditions 5 times and then move to the next row.
So how am I to know if AD15 needs to be 4

I thought we wanted to tell AD15 if the criteria in column W and X were met. But now it sounds like the object here is to get Column AD to a certain number
 
Upvote 0
Hi 'My Answer Is This'.

AD15 adjusts W15 & X15.
Same as AD16 adjusts W16 & X16.

Every time you increment AD15, W15 will increase and X15 will decrease.
AD15 needs to be increased by increments of 1 until W15 is >=$W$12 and X15<=$X$12.
 
Upvote 0

Forum statistics

Threads
1,217,400
Messages
6,136,402
Members
450,009
Latest member
Office_Drone

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top