If/Then in VBA

jardenp

Active Member
Joined
May 12, 2009
Messages
367
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.
 

Some videos you may like

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

jimmy90

Board Regular
Joined
May 1, 2013
Messages
80
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
 

jardenp

Active Member
Joined
May 12, 2009
Messages
367
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.
 

Dryver14

Well-known Member
Joined
Mar 22, 2010
Messages
2,396
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"
 

Watch MrExcel Video

Forum statistics

Threads
1,122,577
Messages
5,596,996
Members
414,116
Latest member
sfullnet

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