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.
 
Sorry. I was being malcoordinated.
Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    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

Regards
Adam
 
Upvote 0

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.
Try

Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    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
Thank you both. Adam where in Southampton are you? I am in Southampton too!
 
Upvote 0
Small world hey. I live by Dock Gate 4 (the actual dock gate 4, not the nightclub). I work at the moment in Chandler's Ford although I'm changing job next week to work between Romsey & Winchester.

You?

Regards
Adam
 
Upvote 0
Sorry I noticed one slight error. It needs to start in row 2 and not affect the headers in row 1.

I work in Portswood and live in Shirley.
 
Upvote 0
Sorry I noticed one slight error. It needs to start in row 2 and not affect the headers in row 1.

I work in Portswood and live in Shirley.

Change the loop so i starts at 2.

Will drop you a PM next time we're headed out for some beer!

Regards
Adam
 
Upvote 0
Hi again Adam/Vog. Could the code work in the reverse. If the last 3 in E are the following can the words be put in R?

E R

HAT - Hatch
SAL - Saloon
COU - Coupe
EST - Estate
VAN - Van
MPV - MPV
CON - Cabrio
 
Upvote 0
Yeah. I'd drop them into a lookup table somewhere then reference them at your leisure.
[R2] =VLOOKUP(RIGHT(E2, 3), MyLookupSheet!$A$2:$B$8, 2, False)

Regards
Adma
 
Upvote 0
Try

Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    Select Case Right(Range("E" & i).Value, 3)
        Case "HAT": Range("R" & i).Value = "Hatch"
        Case "SAL": Range("R" & i).Value = "Saloon"
        Case "COU": Range("R" & i).Value = "Coupe"
        Case "EST": Range("R" & i).Value = "Estate"
        Case "Van": Range("R" & i).Value = "Van"
        Case "MPV": Range("R" & i).Value = "MPV"
        Case "CON": Range("R" & i).Value = "Cabrio"
    End Select
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,175
Members
449,071
Latest member
cdnMech

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