if statement in VBA code

tpkelley_no

Board Regular
Joined
Oct 14, 2011
Messages
183
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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Worf

Well-known Member
Joined
Oct 30, 2011
Messages
4,232
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,195,600
Messages
6,010,651
Members
441,558
Latest member
lambierules

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
Top