if statement in VBA code

tpkelley_no

Board Regular
Joined
Oct 14, 2011
Messages
188
Office Version
  1. 2010
Platform
  1. Windows
I need the following formula in VBA code =if(E242<>0,D242+E242,0). The formula needs to be placed in column “I”. Where column “B” has “LB” and “LB” can be in any row in column “B” I came up with the following and it did not work.

Code:
Sub test()
Dim i As Long, LRT As Long
LRT = Range("B" & Rows.Count).End(xlUp).Row
For i = LRT To 1 Step -1
If Cells(i, 2).Value = "LB" Then
Cells(i, 8).Formula = "=if(" & Range("b6:b" & LRT <> 0)("d6:d" & LRT + "e6:e" & LRT).Address&
End If
Next
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
This code will place the same formula on all rows, is that what you want?

Code:
Sub test()
Dim i As Long, LRT&
LRT = Range("B" & Rows.Count).End(xlUp).Row
For i = LRT To 1 Step -1
    If Cells(i, 2) = "LB" Then Cells(i, 9).Formula = "=if(e242<>0,d242+e242,0)"
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,186
Members
449,071
Latest member
cdnMech

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