apply if formula to column range

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
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".
 
What is your expected outcome in each separate cell in column E?
Sorry, I'm terrible at explaining what I'm trying to accomplish.

If E is blank and C is blank then LEFT(B,2)
If E is blank and C is not blank then LEFT(C,3)

2021_CertReadinessReport_87_87STX_LA_10302021.xls
BCDE
3BCDE
400 Total
500 Group405C16Data
600 Group405C17Data
700 Group00a group425A01
800 Group425A05Data
900 Group425A06Data
1000 Group425A10Data
1100 Group00b group425A16
1200 Group425A61Data
1300 Group425A76Data
Dealer Status
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
It must be me, but I'm totally lost. From your minisheet I can't tell what the (original) starting situation is and what the final desired situation should be.
If you look at the minisheet of my post #10 you will hopefully see some correlations between what's in columns B and C on the one hand, and the result in target column E on the other. Such an interrelation cannot be deduced from the mini-sheet you posted.
 
Upvote 0
It must be me, but I'm totally lost. From your minisheet I can't tell what the (original) starting situation is and what the final desired situation should be.
If you look at the minisheet of my post #10 you will hopefully see some correlations between what's in columns B and C on the one hand, and the result in target column E on the other. Such an interrelation cannot be deduced from the mini-sheet you posted.
You're right, you're lost because I'm terrible. The first sheet is the before the macro is run, here is what it would look like after.
2021_CertReadinessReport_87_87STX_LA_10302021.xls
BCDE
3BCDE
400 Total00
500 Group405C16Data
600 Group405C17Data
700 Group00a group425A0100a
800 Group425A05Data
900 Group425A06Data
1000 Group425A10Data
1100 Group00b group425A1600b
1200 Group425A61Data
1300 Group425A76Data
Dealer Status
 
Upvote 0
Ok, how about ...

VBA Code:
Dim r As Range
For Each r In Range("E3:E" & Range("B" & Rows.Count).End(xlUp).Row)
    If Len(r.Value) = 0 Then
        If r.Offset(, -2).Value = "" Then
            r.Value = Left(r.Offset(, -3).Value, 2)
        Else
            r.Value = Left(r.Offset(, -2).Value, 3)
        End If
        r.HorizontalAlignment = xlLeft
    End If
Next
 
Upvote 0
Solution
Ok, how about ...

VBA Code:
Dim r As Range
For Each r In Range("E3:E" & Range("B" & Rows.Count).End(xlUp).Row)
    If Len(r.Value) = 0 Then
        If r.Offset(, -2).Value = "" Then
            r.Value = Left(r.Offset(, -3).Value, 2)
        Else
            r.Value = Left(r.Offset(, -2).Value, 3)
        End If
        r.HorizontalAlignment = xlLeft
    End If
Next
As you can imagine what you wrote works perfectly, thank you. Can you help me understand why in one line you have value = 0 and in another you have value = ""?
 
Upvote 0
You are welcome and thanks for letting me know.

Can you help me understand why in one line you have value = 0 and in another you have value = ""?
No special reason, just two different ways of doing the same thing /getting the same result. Although I try to be as consistent as possible in what I'm coding, I've paid less attention to that in this situation, moreover and meanwhile dealing with the question: "what exactly does @288enzo want?"

BTW, I didn't use Value = 0, I used Len(Value) = 0 and that's quite something different.

To determine whether a String variable contains characters at all, you can test whether the length of that text string is greater than 0, and that's what I did in the first comparison.

Other ways are:
If s = "" If s = vbNullString

With a worksheet range there is a fourth option:
If IsEmpty(Range("A1").Value)
 
Upvote 0
You are welcome and thanks for letting me know.


No special reason, just two different ways of doing the same thing /getting the same result. Although I try to be as consistent as possible in what I'm coding, I've paid less attention to that in this situation, moreover and meanwhile dealing with the question: "what exactly does @288enzo want?"

BTW, I didn't use Value = 0, I used Len(Value) = 0 and that's quite something different.

To determine whether a String variable contains characters at all, you can test whether the length of that text string is greater than 0, and that's what I did in the first comparison.

Other ways are:
If s = "" If s = vbNullString

With a worksheet range there is a fourth option:
If IsEmpty(Range("A1").Value)
So much to learn, thank you for taking the time to explain.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

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