If/Then in VBA

jardenp

Active Member
Joined
May 12, 2009
Messages
373
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
I would like VBA code that evaluates A1 according to the formula LEFT(A1,3)="Ven". If it is true, I want B1, C1, and D1 to remain blank. If it is false, then it applies these formulas: in B1, "=F1*4"; in C1, "=G1*4"; and in D1, "=H1*4".

It would apply this IF/THEN to all rows in the sheet.

I know how to do this with IF formulas, but I need the B, C, and D cells to remain blank if A starts with "Ven" because I want the full A cell text string to display. If there is any formula in B,C, or D, the text display is cut off, even when I set the cell value to "" in the IF formula and then cut and paste for values only. I can't widen column A because it would be too wide.

Another alternative would be to fill in with IF formulas, copy/paste to values and then somehow delete the B,C, and D cells for all rows where the A cell started with "Ven".

Any help would be greatly appreciated.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Code:
Dim OtherCells As Range
For Each c In Range("A1:A10")
    If UCase(Left(c, 3)) = "VEN" Then
        Set OtherCells = Range(c.Offset(, 1), c.Offset(, 3))
        OtherCells.Clear
    Else
        R = c.Row
        Range("B" & R).Formula = "=F"&R&"*4"
        Range("C" & R).Formula = "=G"&R&"*4"
        Range("D" & R).Formula = "=H"&R&"*4"
    End If
Next c
 
Upvote 0
I get a syntax error for the lines starting with "Range." And since code like that is beyond my skills, I'm not sure why.

Nonetheless, I'm impressed. Thanks.
 
Upvote 0
The code is good Purely a matter of spacing

Code:
        R = c.Row
        Range("B" & R).Formula = "=F" & R & "*4"
        Range("C" & R).Formula = "=G" & R & "*4"
        Range("D" & R).Formula = "=H" & R & "*4"
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,034
Members
448,543
Latest member
MartinLarkin

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