apply if formula to column range

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
723
Office Version
  1. 2016
Platform
  1. Windows
I have a with statement that is specific to one cell, I would like to apply it to the column.
VBA Code:
    With Range("E4")
    .Formula = "=IF(C4="""",LEFT(B4,3),LEFT(C4,3))"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    End With
This is what I came up with, but where I think I'm having an issue is where I'm using a specific cell for the formula and value.
VBA Code:
With Range("E4:E" & Range("B" & Rows.Count).End(xlUp).Row).Select
    If Value = """" Then
    .Formula = "=LEFT(B4,3)"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    Else
    .Formula = "=LEFT(C4,3)"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    End With
End Sub
I'm getting an error I can't have an end with without with. My statement starts with "with".
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Put a missing End If right above the End With.
 
Upvote 0
Put a missing End If right above the End With.
Thanks, I made a few changes and am getting the same end with without with
VBA Code:
With Range("E4:E" & Range("B" & Rows.Count).End(xlUp).Row).Select
    If Value = """" Then
    .Formula = "=LEFT(B4,3)"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    Else
    If Value = """" Then
    .Formula = "=LEFT(C4,3)"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    End If
End With
 
Upvote 0
You made just one change too many. Keep the red colored one (as I suggested) & get rid of the striked one ...

Rich (BB code):
With Range("E4:E" & Range("B" & Rows.Count).End(xlUp).Row).Select
    If Value = """" Then
    .Formula = "=LEFT(B4,3)"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    Else
    If Value = """" Then
    .Formula = "=LEFT(C4,3)"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    End If
End With
 
Upvote 0
You made just one change too many. Keep the red colored one (as I suggested) & get rid of the striked one ...

Rich (BB code):
With Range("E4:E" & Range("B" & Rows.Count).End(xlUp).Row).Select
    If Value = """" Then
    .Formula = "=LEFT(B4,3)"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    Else
    If Value = """" Then
    .Formula = "=LEFT(C4,3)"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    End If
End With
I had tried that after your suggestion, but got the message "object required" which made me think I need another "if".
VBA Code:
With Range("E4:E" & Range("B" & Rows.Count).End(xlUp).Row).Select
    If Value = """" Then
    .Formula = "=LEFT(B4,3)"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    Else
    .Formula = "=LEFT(C4,3)"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    End If
End With
 
Upvote 0
Hadn't looked closely at your code before, but what you're probably trying to achieve won't work this way (with temporary use of formulas), at least not in a relatively easy to code way.
Alternatively you could use the code below

VBA Code:
Dim r As Range
For Each r In Range("E4:E" & Range("B" & Rows.Count).End(xlUp).Row)
    If r.Offset(, -2).Value = "" Then
        r.Value = Left(r.Offset(, -3).Value, 3)
    Else
        r.Value = Left(r.Offset(, -2).Value, 3)
    End If
    r.HorizontalAlignment = xlLeft
Next
 
Upvote 0
I removed .Select and after running it all data in E was cleared :(
VBA Code:
With Range("E4:E" & Range("B" & Rows.Count).End(xlUp).Row)
    If Value = """" Then
    .Formula = "=LEFT(B4,3)"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    Else
    .Formula = "=LEFT(C4,3)"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    End If
End With

I also tried it like this, and had the same results.
VBA Code:
With Range("E4:E" & Range("B" & Rows.Count).End(xlUp).Row)
    If Value = """" Then
    .Formula = "=LEFT(B4,3)"
'    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    Else
    .Formula = "=LEFT(C4,3)"
'    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    End If
End With
 
Upvote 0
What is your expected outcome in each separate cell in column E?
 
Upvote 0
Hadn't looked closely at your code before, but what you're probably trying to achieve won't work this way (with temporary use of formulas), at least not in a relatively easy to code way.
Alternatively you could use the code below

VBA Code:
Dim r As Range
For Each r In Range("E4:E" & Range("B" & Rows.Count).End(xlUp).Row)
    If r.Offset(, -2).Value = "" Then
        r.Value = Left(r.Offset(, -3).Value, 3)
    Else
        r.Value = Left(r.Offset(, -2).Value, 3)
    End If
    r.HorizontalAlignment = xlLeft
Next
I like what you did using offset, thank you. The problem is that your code also clears out all data in E.

I would have used ISBLANK, but for whatever reason the cell that looks blank isn't technically blank. I ran ISBLANK and the cell came up with FALSE, so I have confirmed something is there. That's why the IF statement is IF(E4="""") THEN

My original code was specific to one cell:
VBA Code:
    With Range("E4")
    .Formula = "=IF(C4="""",LEFT(B4,3),LEFT(C4,3))"
    .Value = Range("E4")
    .HorizontalAlignment = xlLeft
    End With

I'm trying to apply the above to an entire range in column E.

Hopefully that makes more sense.

I'm terrible at explaining.

Basically, if any cell in Range("E4:E" & Range("B" & Rows.Count).End(xlUp).Row) is equal to "" then FORMULA.

Thank you
 
Upvote 0
I like what you did using offset, thank you. The problem is that your code also clears out all data in E.

I'm just guessing since I don't know what you're trying to achieve. My post #6 code is my best guess as a derivative of your code in post #1 and results in ...
Book1
BCDE
4B4C4C4
5B5B5
6B6C6C6
7B7B7
8B8C8C8
9B9C9C9
ENZO
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,853
Members
449,194
Latest member
HellScout

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