If "text" then "formula" in another cell VBA

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
150
Hello,

I need help with an if then vba -

I have either WRSTEA or WRSEXC in column F, I need to have the corresponding formula populate in column R or S.

Example:
If F2 = WRSTEA, then R2 = Blank, S2 = (O2*0.0295)
If F5 = WRSEXC, then R2 = (O5*0.081), S2 = Blank

Thanks!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I assumed that your second example, if F5 was WRSEXC that you were modifying R5 and S5, not R2 and S2.

Code:
Sub test()
Dim c As Range
For Each c In Range("F2:F" & Range("F" & Rows.Count).End(xlUp).Row)
Select Case UCase(c)
    Case "WRSTEA": Cells(c.Row, "R") = "": Cells(c.Row, "S").Formula = "=" & Cells(c.Row, "O").Address(0, 0) & "*0.0295"
    Case "WRSEXC": Cells(c.Row, "S") = "": Cells(c.Row, "R").Formula = "=" & Cells(c.Row, "O").Address(0, 0) & "*0.081"
    Case Else: c.Offset(, 12).Resize(, 2).ClearContents
End Select
Next
End Sub
 
Last edited:
Upvote 0
I assumed that your second example, if F5 was WRSEXC that you were modifying R5 and S5, not R2 and S2.

Code:
Sub test()
Dim c As Range
For Each c In Range("F2:F" & Range("F" & Rows.Count).End(xlUp).Row)
Select Case UCase(c)
    Case "WRSTEA": Cells(c.Row, "R") = "": Cells(c.Row, "S").Formula = "=" & Cells(c.Row, "O").Address(0, 0) & "*0.0295"
    Case "WRSEXC": Cells(c.Row, "S") = "": Cells(c.Row, "R").Formula = "=" & Cells(c.Row, "O").Address(0, 0) & "*0.081"
    Case Else: c.Offset(, 12).Resize(, 2).ClearContents
End Select
Next
End Sub

Scott, this works perfectly. Thanks so much!
And yes you were correct to assume the corresponding cells for the 2nd formula.
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,037
Members
449,205
Latest member
Eggy66

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