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.
 
Hi. I have added the rows in yellow but it gets an error on the 'else' coloured orange. Any ideas please?

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"
ElseIf Range("R" & i).Value = "ATV" Then
Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
& "EST"
End If
ElseIf Range("R" & i).Value = "SUV" Then
Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
& "EST"
End If
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

I just need it foolproof so I can add lines myself when I come across any eventuality instead of bothering you more.
 
Last edited:
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
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
        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 = "ATV" Then
                Range("E" & i).Value = Left(Range("E" & i).Value, Len(Range("E" & i).Value) - 3) _
                & "EST"
            ElseIf Range("R" & i).Value = "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
Thanks Vog, I think my thread is victim of some spamming.
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,777
Members
449,049
Latest member
greyangel23

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