VBA if cell value is greater than another cell value then syntax

PPriest

New Member
Joined
Jun 11, 2018
Messages
36
I am struggling to find any solution to my specific need online. I am tying to find the correct syntax to do the following:

I want to create a macro that increases the value in one cell (W9) by 25 providing it doesn't go over the value in another cell (W7).

I then want to create a second macro to decrease the value in one cell (W9) by 25 until it reaches 1.

Here is what I was thinking - not sure if I'm on the right track or not but I can't seem to find the right syntax to deliver any results other than an error:

If Range("W7").Value > (Range("W9").Value+25) Then
Range("W9").Value = Range("W9")+25)
ElseIf Range("W7").Value < (Range("W9").Value+25) Then
End If
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Code:
If (Range("W9").Value + 25) <= Range("W7").Value Then
     Range("W9") = Range("@9").Value + 25
End If

Code:
For i = 1 to Int(Range("W9").value/25)
    If Range("W9").Value Mod 25 = 0 Then
      Range("W9").Value = 1
    Exit For
 End If
Next
 
Last edited:
Upvote 0
I was not 100% sure of your first requirement (do we stop at the number before adding 25 if adding 25 takes us past the value in W7 or do we put W7's value in W9 if adding 25 takes it past W7's value).

If we stop before W7's value
-----------------------------------------------
Code:
Sub IncreaseW9By25DoNotExceedW7()
  [W9] = [IF(W9+25<=W7,W9+25,W9)]
End Sub

If we stop at W7's value
-----------------------------------------------
Code:
Sub IncreaseW9By25StopAtW7()
  [W9] = [MIN(W9+25,W7)]
End Sub

For your second requirement, try this...
Code:
Sub DecreaseW9By25MinValueOfOne()
  [W9] = [MAX(1,W9-25)]
End Sub
 
Last edited:
Upvote 0
I just noticed that I didn't finish the second macrol


Code:
For i = 1 to Int(Range("W9").value/25)
    If Range("W9").Value Mod 25 = 0 Then
        Range("W9").Value = 1
        Exit For
    End If
    If Range("W9").Value > 25 Then Range("W9") = Range("W9").Value - 25    
Next
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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