Formula or Macro? If, Find, Len?

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have column E. Inside it is various length text but I need it to concentrate on the last 3 digits. I need it to replace it with the first 3 characters in column R.

e.g column E may have NIL_ALL in it so I need it to look and see if anything is in column R and if there is replace 'ALL' with the first 3 in R, which may be Hatch so it will become NIL_HAT in E. If there is nothing in R (in the same row of course) then do nothing. A macro would be better where it would replace what is in E, however I could put a helper column in with a formula. Thanks.

Just to confuse things if the word cabrio is found in R then I need the 3 letters to be CON not CAB, if this provides a problem I can do a find/replace after.
 
Thanks again, VoG yours did it this time!
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
This code works fine that was given in this thread but if the cell in column R is blank can the last 3 in column E be changed to 'ALL' please.

My first post explains what this code does. Thanks.

Code:
Sub MyMacro18()
Dim lr As Long, i As Long
lr = Range("E" & Rows.Count).End(xlUp).Row
For i = lr To 2 Step -1
    If Range("R" & i).Value <> "" Then Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
    & UCase(Left(Replace(Range("R" & i).Value, "Cabrio", "CON"), 3))
Next i
End Sub
 
Upvote 0
Try

Code:
Sub MyMacro18()
Dim lr As Long, i As Long
lr = Range("E" & Rows.Count).End(xlUp).Row
For i = lr To 2 Step -1
    If Range("R" & i).Value <> "" Then
        Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
        & UCase(Left(Replace(Range("R" & i).Value, "Cabrio", "CON"), 3))
    Else
        Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
        & "ALL"
    End If
Next i
End Sub
 
Upvote 0
Thanks but I get a runtime error 5 and it debugs here

Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
& UCase(Left(Replace(Range("R" & i).Value, "Cabrio", "CON"), 3))
 
Upvote 0
That would happen if the cell in column E contained less than 3 characters. Maybe

Code:
Sub MyMacro18()
Dim lr As Long, i As Long
lr = Range("E" & Rows.Count).End(xlUp).Row
For i = lr To 2 Step -1
    If Range("R" & i).Value <> "" Then
        If Len(Range("E" & i).Value) >= 3 Then
            Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
            & UCase(Left(Replace(Range("R" & i).Value, "Cabrio", "CON"), 3))
        Else
            Range("E" & i).Value = Range("R" & i).Value
        End If
    Else
        If Len(Range("E" & i).Value) >= 3 Then
            Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) & "ALL"
        Else
            Range("E" & i).Value = "ALL"
        End If
    End If
Next i
End Sub
 
Upvote 0
Hi VoG. I need a line of code added to the code in post 25 which is working well for me. If the data in column R is ATV/SUV then make the last 3 in column 'E' EST please. A bit like the cabrio - CON scenario. Thanks.

Once this other line of code is added like the cabrio line I should be able to see what I need to copy if another scenario arises and add myself.
 
Upvote 0
Maybe this - I've just used an If rather than trying a nested Replace

Code:
Sub MyMacro18()
Dim lr As Long, i As Long
lr = Range("E" & Rows.Count).End(xlUp).Row
For i = lr To 2 Step -1
    If Range("R" & i).Value <> "" Then
        If Len(Range("E" & i).Value) >= 3 Then
            If Range("R" & i).Value = "ATV/SUV" Then
                Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
                & "EST"
            Else
                Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
                & UCase(Left(Replace(Range("R" & i).Value, "Cabrio", "CON"), 3))
            End If
        Else
            Range("E" & i).Value = Range("R" & i).Value
        End If
    Else
        If Len(Range("E" & i).Value) >= 3 Then
            Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) & "ALL"
        Else
            Range("E" & i).Value = "ALL"
        End If
    End If
Next i
End Sub
 
Upvote 0
So if I need to add more could you highlight the lines I would need to copy and obviously change the values accordingly. Thanks.
 
Upvote 0
I've added a made up example

Rich (BB code):
Sub MyMacro18()
Dim lr As Long, i As Long
lr = Range("E" & Rows.Count).End(xlUp).Row
For i = lr To 2 Step -1
    If Range("R" & i).Value <> "" Then
        If Len(Range("E" & i).Value) >= 3 Then
            If Range("R" & i).Value = "ATV/SUV" Then
                Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
                & "EST"
            ElseIf Range("R" & i).Value = "XXX/YYY" Then
                Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
                & "ZZZ"
            Else
                Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
                & UCase(Left(Replace(Range("R" & i).Value, "Cabrio", "CON"), 3))
            End If
        Else
            Range("E" & i).Value = Range("R" & i).Value
        End If
    Else
        If Len(Range("E" & i).Value) >= 3 Then
            Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) & "ALL"
        Else
            Range("E" & i).Value = "ALL"
        End If
    End If
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,022
Members
448,939
Latest member
Leon Leenders

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