VBA increment value until a condition is met

RPM7

Board Regular
Joined
Nov 28, 2007
Messages
191
Would anyone be able to help me with the following VBA?
I've no idea of how to do it.

If D15 < K15, then increment Y15 value+1 (This would need to loop until the condition is met)
Then go to next row and check the same.

This would have to apply to rows 15 to 44

Thanks
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
If I am right, you want to trigger change in D15 and K15, to see if D15<K15?
What is in D15 and K15, formula or value inputed manually?
 
Upvote 0
If I am right, you want to trigger change in D15 and K15, to see if D15<K15?
What is in D15 and K15, formula or value inputed manually?
Hi @bebo021999, thanks for the quick reply.

I only want to trigger a change to Y15. This cell is a manually inputted value.
D15 and K15 are formula driven cells and will update when Y15 changes.
 
Upvote 0
Try :
VBA Code:
Sub v()
[Y15:Y44] = Evaluate("IFERROR(IF(D15:D44<K15:K44,(Y15:Y44)+1,Y15:Y44),"""")")
End Sub
 
Upvote 0
Having read your post #3, maybe this :
VBA Code:
Sub v()
[Y15:Y44] = Evaluate("IF(D15:D44<K15:K44,Y15:K44+K15:K44-D15:D44,Y15:Y44)")
End Sub
 
Upvote 0
Having read your post #3, maybe this :
VBA Code:
Sub v()
[Y15:Y44] = Evaluate("IF(D15:D44<K15:K44,Y15:K44+K15:K44-D15:D44,Y15:Y44)")
End Sub
Thanks footoo. Unfortunately, that version is changing the Y cells into multiple decimal places instead of just incrementing as an integer.
It also stopped prematurely on some of the rows where the condition wasn't met.
 
Upvote 0
What are the formulas, results and formatting in D15 and K15?
 
Upvote 0
What are the formulas, results and formatting in D15 and K15?
It's a bit complicated unfortunately.

K15 is linked to a value in another worksheet, it's format is (Number).

D15 is calculated by a lot of stacked formulas on the current worksheet. All of these formulas are updated when Y15 is changed.
D15's format is (Number)

Y15 is a static number, but the cell's format is currently (General)
 
Upvote 0
Try this (untested) :
VBA Code:
Sub vv()
Dim cel As Range, x%
Do
    x = 0
    For Each cel In Range("Y15:Y44")
        If cel.Offset(0, -21).Value < cel.Offset(0, -14).Value Then
            cel = cel + 1
        Else: x = x + 1
        End If
    Next
Loop While x < 30
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,934
Messages
6,175,487
Members
452,648
Latest member
Candace H

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