Replace String in Cell Faster

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,461
I have data that resembles this format:

GX14-526544
RF14-26547
UC14-659681

I am trying to replace these strings in each cell so they are now:

526544
26547
659681

I am using this method currently:
Code:
[COLOR=#0000ff]For[/COLOR] x = 2 [COLOR=#0000ff]To [/COLOR]LastRow [COLOR=#0000ff]Step[/COLOR] 1
     Cells(x, 2).Value = Right(Cells(x, 2).Value, Len(Cells(x, 2).Value) - InStr(Cells(x, 2).Value, "-"))
 [COLOR=#0000ff]  Next [/COLOR]x

The problem is that it is taking an extensive period of time. I have already set ScreenUpdating = False. Is there a way to do this string replacement all at once without having to cycle through each individual cell? My data contains several thousand rows.

Any advice or help would be much appreciated. Thanks!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Code:
Cells(1, 2).EntireColumn.Offset(0, 1).InsertWith Range(Cells(2, 3), Cells(LastRow, 3))
    .FormulaR1C1 = "=RIGHT(RC[-1],LEN(RC[-1])-FIND(""-"",RC[-1]))"
    .Value = .Value
End With
Cells(1, 2).EntireColumn.Delete

Try that.
 
Upvote 0
NeonRedSharpie,

Thanks for the advice. Using the formula works well.

TerryHogarth21
,

This works as well!

I appreciate the help. It's funny how simple things can be sometimes...


This is what I ended up going with:

Code:
   Range("C2:C" & LastRow).FormulaR1C1 = "=NUMBERVALUE(RIGHT(RC[-1],LEN(RC[-1])-FIND(""-"",RC[-1])))"
   Range(Cells(2, 3), Cells(Lastrow, 3)).Copy
   Range(Cells(2, 3), Cells(Lastrow, 3)).PasteSpecial Paste:=xlPasteValues
   Columns(2).EntireColumn.Delete
 
Last edited:
Upvote 0

Forum statistics

Threads
1,203,462
Messages
6,055,562
Members
444,799
Latest member
CraigCrowhurst

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