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

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
577
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.
 

Some videos you may like

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,470
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
      Cl.Value = Left(Cl.Value, InStrRev(Cl.Value, "-")) & " By Air"
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
48,400
Office Version
  1. 365
Platform
  1. Windows
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
 

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
577
Office Version
  1. 2010
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,470
Office Version
  1. 365
Platform
  1. Windows
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.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
48,400
Office Version
  1. 365
Platform
  1. Windows
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?
 

Watch MrExcel Video

Forum statistics

Threads
1,127,422
Messages
5,624,699
Members
416,042
Latest member
Oden

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
Top