[VBA] Replace everything after the rightmost hyphen ( - ) in string within filtered range.

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
788
Office Version
  1. 2010
Platform
  1. Windows
Hello hello, possibly the last question for the year for me.

VBA Code:
Set Rng = Range("D3:D" & Lastrowsd + LastrowCT - 2)
    
    For Each cl In Rng.SpecialCells(xlCellTypeVisible)
        ' replace everything after " - " with "By Air"
    Next cl

The cells in this filtered range can have anything in them:

Classical Andalucia - Heathrow
Andre Rieu - Live in Birmingham - Dublin


Etc etc

What I want to do, is have something loop down the list and replace everything after the rightmost hyphen with "By Air"

So it would look like:

Classical Andalucia - By Air
Andre Rieu - Live in Birmingham - By Air



Everything online appears to be about REMOVING the text, not REPLACING it, even when I google for "Replace" - which is weird.

Thanks.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
How about
VBA Code:
      Cl.Value = Left(Cl.Value, InStrRev(Cl.Value, "-")) & " By Air"
 
Upvote 0
Edit: Oops, a bit slower typing than Fluff. :biggrin:

I have assumed all the relevant visible cells will have a "-" in them. Try

VBA Code:
For Each cl In Rng.SpecialCells(xlCellTypeVisible)
    With cl
      .Value = Left(.Value, InStrRev(.Value, "-")) & " By Air"
    End With
Next cl
 
Upvote 0
Haha, thanks guys. I tested Fluff's and it works. Forgot about InStrRev!

Only thing is, this particular set of data is 600 rows long within a dataset of about 10,000 rows - appears to take a few minutes to replace as it's evaluating each line. Is there possibly a faster way? Cheers.
 
Upvote 0
If the filter criteria is always the same, Evaluate might be faster, but I doubt it would be a significant improvement and could even be slower.
 
Upvote 0
this particular set of data is 600 rows long within a dataset of about 10,000 rows - appears to take a few minutes to replace as it's evaluating each line. Is there possibly a faster way?
Are the 600 rows likely to be in a single contiguous range (or small number of such ranges) or are the visible rows scattered completely throughout the 10,000 row range?
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,028
Members
448,940
Latest member
mdusw

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