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.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Perhaps this formula:
=IF(R2="", E2, LEFT(E2, LEN(E2)-3)&UPPER(LEFT(SUBSTITUTE(R2, "Cabrio", "CON"), 3)))

Regards
Adam
 
Upvote 0
Try this

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(Range("R" & i).Value, 3))
Next i
End Sub
 
Upvote 0
Or this code?
Code:
Option Explicit
 
Public Sub CheckEntries()
 
    Dim rng, r As Range, s As String
 
    Set rng = ActiveSheet.Range("E2:E5") 'change as appropriate
 
    For Each r In rng
 
        If r.Value <> "" And r.Offset(0, 13).Value <> "" Then
            s = Left(r.Value, Len(r.Value) - 3) & UCase(Left(Replace(r.Offset(0, 13).Value, "Cabrio", "CON"), 3))
            r.Value = s
        Else
            'do nothing
        End If
 
    Next r
 
End Sub

Regards
Adam
 
Upvote 0
Thank you both. I assume with your 2 codes I will need to change CON/CAB manually?
 
Upvote 0
Mine should catch it.

Regards
Adam

It does thanks. Could you adapt yours a little please Adam. I need it a bit like VoGs where it counts the range automatically so I can use it on all different size files, rather than changing the range manually each time. Thanks.
 
Upvote 0
Use VOG's; it's neater. Just drop my formula in to his (not tested):
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 <> "" '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
That doesnt work. When I put it in the module some of the writing has gone red.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,378
Members
448,955
Latest member
BatCoder

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