How to use an IF formula in VBA

lmichaluk

New Member
Joined
Aug 4, 2011
Messages
9
Hi there - very new to excel and trying to write a macro that will sum the cells above and then will not let the answer exceed a given amount. For example: the "sum macro" would be something like
Sub b8()
Range("b8").Formula = "=b6+b7"
End Sub

The "If" statement would be something like =IF(B8<100.1,B8,100)

So. How do I include the If statement in the macro?
Thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Assign a new variable iSum = b6+b7

Code:
If iSum < 100.1 Then
Range("b8").Formula = iSum ' Or write like "=b6+b7"
Else
Range("b8").Formula = 100
End If
 
Upvote 0
Brilliant. Thanks.

Sorry - one more question. Is there a way, in the macro, that I can allow for the user to add a row; i.e. the first range is B6+B7; if the user adds two more rows, how do I configure the macro so that it will capture B6+B7+B8+B9...if it was autosum I would use B6:B9. How would that work here?
 
Upvote 0
Try like this

Code:
Dim LR As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
Range("B" & LR + 1).Formula = "=MIN(100,B6+B" & LR & ")"
 
Upvote 0
Try like this

Code:
Dim LR As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
Range("B" & LR + 1).Formula = "=MIN(100,B6+B" & LR & ")"

Thanks Peter - just tried that and I get a "compile error - Invalid outside procedure" with the (x1Up) shaded.
Linda
 
Upvote 0
Thanks for your help Peter. I did copy and paste, but that is the error I get...I also just tried to to copy the statement from your second note, but it automatically changes to the 1. Is there a symbol key or something that I should use? I notice that your note as displayed on my system shows a 1 as well. Wierd...
Thanks again for persevering.
Linda
 
Upvote 0
Well, that is a new one on me. Do you have a sub called 1 or l? Or any Auto text feature like that?

Anyway it has to be the letter L not the number 1.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

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