VBA to remove characters from a comma separated list, where certain characters are found?

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
790
Office Version
  1. 365
Platform
  1. Windows
Hi all, need fast help on this as I'm under a timescale.

I have a list of ads due to be placed in a paper and under the "Pickups" for coach travel there is meant to be a list of coach-only pickups, but air and rail have slipped in.

As an example, please see the below:

Code:
[COLOR=#ff0000]Bury St Edmunds, Colchester, Ipswich, Flying from Norwich (NWI), Flying from Stansted (STN)[/COLOR]

So I need to remove everything after "Ipswich", including that last comma, I need to find the first instance of "Flying" and remove everything using that as a start point, to end up with the following:

Code:
[COLOR=#008000]Bury St Edmunds, Colchester, Ipswich[/COLOR]

This is more or less simple enough to do with FIND etc, but the problem is that sometimes the Airport or Rail pickups are buried in the middle, like this:

Code:
[COLOR=#ff0000]Birmingham, Flying from Birmingham (BHX), (RS) Birmingham New Street, Solihull, Sutton Coldfield[/COLOR]

Which would then need to be like this:

Code:
[COLOR=#008000]Birmingham, Solihull, Sutton Coldfield[/COLOR]


Can anyone help me urgently, I have to detect whether "Flying" or "(RS)" is within a set of commas (or at the end!) and then remove just those pickups including their respective commas to end up with just coach pickups (these don't have "(RS)" or "Flying from xxxx" in them)


Really appreciate it, thanks!
 
Last edited:
Re: [VBA] [Urgent] How to remove characters from a comma separated list, where certain characters are found?

That looks fine :)

Just tried the three variants on this string:

Code:
Glasgow, Flying from Glasgow (GLA), (RS) Glasgow Central, Flying from Edinburgh (EDI), (RS) Edinburgh

Every time, for Coach, Rail and Air I get the following result:

Code:
Glasgow


Seems odd, has been working perfectly up until this string!


EDIT: For some reason, the vbTextCompare = 1 had been reset to 0 on all counts.

I did not do this change and nobody else uses the sheets. Is this possible for the Function code to reset?
 
Last edited:
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Re: [VBA] [Urgent] How to remove characters from a comma separated list, where certain characters are found?

No, the code cannot change itself.
 
Upvote 0

Forum statistics

Threads
1,215,394
Messages
6,124,683
Members
449,180
Latest member
kfhw720

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