Remove everything between commas if string matches?

angio

New Member
Joined
Jan 10, 2011
Messages
5
Hello,

I'm hoping someone could help or show me a macro that would do this.

In B2 I have the following

{165}Finishes: S[{2297}Polished Brass,{2298}Raw Brass,{2299}Brushed Brass,{2300}Rubbed Brass,{2301}Antique Brass,{2302}Weathered Bronze,{2303}Pewter,{2304}Polished Chrome,{2305}Polished Nickel,{2306}Antique White,{2307}Oil Rubbed Bronze,{2308}Rust,{2309}Matte Black,{2310}Verde]


In C2, D2, E2 I have

Polished Brass Weathered Bronze Pewter

I need a macro that will match C2 D2 E2 in B2 keep everything between the commas and inside the brackets [ ] and remove all other entries. So whats left should look like

{165}Finishes: S[{2297}Polished Brass,{2302}Weathered Bronze,{2303}Pewter]

Some entries will have more options past e2 going on to f2 and more. Maybe those entries in columns C2, D2, E2 should be in one cell commas separated to make a macro easier? Any help would be really appreciated.

Thanks
 
Last edited by a moderator:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Instead of removing all commas, just build the statement you want.

Do they always have the same {number} for each item? Have those in a table, and build a concatenate and vlookup formula.

something like:
=vlookup(c2) & C2 & "," & vlookup(d2)...
 
Upvote 0
The ID # before Finishes is 3 digit, all other ID's between the [] are 4

First row
{166}Finishes: S[{2311}Polished Brass,{2312}Raw Brass,{2313}Brushed Brass,{2314}Rubbed Brass,{2315}Antique Brass,{2316}Weathered Bronze,{2317}Pewter,{2318}Polished Chrome,{2319}Polished Nickel,{2320}Antique White,{2321}Oil Rubbed Bronze,{2322}Rust,{2323}Matte Black,{2324}Verde]

Last row
{111}Finishes: S[{1541}Polished Brass,{1542}Raw Brass,{1543}Brushed Brass,{1544}Rubbed Brass,{1545}Antique Brass,{1546}Weathered Bronze,{1547}Pewter,{1548}Polished Chrome,{1549}Polished Nickel,{1550}Antique White,{1551}Oil Rubbed Bronze,{1552}Rust,{1553}Matte Black,{1554}Verde]
 
Upvote 0
Hi
Welcome to the board

This is a udf solution.

In A2: =StripCodes(B2,C2:E2)
In A3: =StripCodes(B3,C3:F3)

(first parameter the text, second parameter a contiguous horizontal vector with one code per cell, no leading or trailing spaces)

Code:
Function StripCodes(sText As String, rCodes As Range) As String
Dim sCodes As String
 
sCodes = Join(Application.WorksheetFunction.Index(rCodes.Value, 0), "|")
With CreateObject("VBScript.RegExp")
    .Pattern = "\{\d{4}\}(?!" & sCodes & ")[a-zA-Z ]+,?"
    .Global = True
    StripCodes = Replace(.Replace(sText, ""), ",]", "]")
End With
End Function

Ex.:


Book1.xlsm
ABCDEF
1
2{165}Finishes: S[{2297}Polished Brass,{2 298}Raw Brass,{2303}Pewter,{23 08}Rust]{165}Finishes: S[{2297}Polished Brass,{2 298}Raw Brass,{2299}Brushed Brass,{2300} Rubbed Brass,{2301}Antique Brass,{2302}W eathered Bronze,{2303}Pewter,{2304}Polis hed Chrome,{2305}Polished Nickel,{2306}A ntique White,{2307}Oil Rubbed Bronze,{23 08}Rust,{2309}Matte Black,{2310}Verde]Polished BrassWeathered BronzePewter
3{123}XYZ: S[{0002}C,{000 3}D,{0005}E,{0006}F,{000 8}H]{123}XYZ: S[{0000}A,{0001}B,{0002}C,{000 3}D,{0004}A,{0005}E,{0006}F,{0007}G,{000 8}H,{0009}I]CEFH
Sheet2
Cell Formulas
RangeFormula
A2A2=StripCodes(B2,C2:E2)
A3A3=StripCodes(B3,C3:F3)
 
Last edited by a moderator:
Upvote 0
Very neat PGC! However, it does have the drawback of having to specify the exact range of populated cells in columns C, D, E etc in each formula since it fails if there are blank cells at the end.

It also fails if there are blank cells at the start or in the middle of the range. Whilst that may be unlikely, it would certainly be better if the user could use a range of columns wide enough to cater for the greatest number of expected columns and just copy the formula down.

Therefore, I suggest this slight addition to your code to cope with blanks, wherever they occur in the rCodes range.

I've also incorporated the idea of only creating the RegEx object once to help with code speed as we discussed in another thread a couple of months ago.


VBA Code:
Function StripCodes(sText As String, rCodes As Range) As String
  Static RegEx As Object
  Dim sCodes As String
  
  If RegEx Is Nothing Then
    Set RegEx = CreateObject("VBScript.RegExp")
    RegEx.Global = True
  End If
  
  sCodes = Join(Application.Index(rCodes.Value, 0), "|")
  
  With RegEx
    .Pattern = "\|+(?=\|)|^\||\|$"
    sCodes = .Replace(sCodes, "")
    .Pattern = "\{\d{4}\}(?!" & sCodes & ")[a-zA-Z ]+,?"
    StripCodes = Replace(.Replace(sText, ""), ",]", "]")
  End With
End Function


Formula in A2 copied down.

Excel Formula:
=StripCodes(B2,C2:H2)

Excel Workbook
ABCDEFGH
2{165}Finishes: S[{2297}Polished Brass,{2302}Weathered Bronze,{2303}Pewter]{165}Finishes: S[{2297}Polished Brass,{2298}Raw Brass,{2299}Brushed Brass,{2300}Rubbed Brass,{2301}Antique Brass,{2302}Weathered Bronze,{2303}Pewter,{2304}Polished Chrome,{2305}Polished Nickel,{2306}Antique White,{2307}Oil Rubbed Bronze,{2308}Rust,{2309}Matte Black,{2310}Verde]Polished BrassWeathered BronzePewter
3{123}XYZ: S[{0002}C,{0005}E,{0006}F,{0008}H]{123}XYZ: S[{0000}A,{0001}B,{0002}C,{0003}D,{0004}A,{0005}E,{0006}F,{0007}G,{0008}H,{0009}I]CEFH
4{123}XYZ: S[{0005}E,{0006}F]{123}XYZ: S[{0000}A,{0001}B,{0002}C,{0003}D,{0004}A,{0005}E,{0006}F,{0007}G,{0008}H,{0009}I]EF
RegEx
 
Last edited:
Upvote 0
Good points, Peter.

The first pattern does not delete all the "|" at the beginning of the string. For ex. "||a" results in "|a" instead of just "a".

Another option for the first pattern:

Code:
        .Pattern = "^\|+|\|+(?=\||$)"
 
Upvote 0
The first pattern does not delete all the "|" at the beginning of the string.
Darn it! :mad:
I thought that I had tested all the different possibilities but was probably feeling too excited that I had actually managed to use a 'lookahead' successfully for about the first time. :)

Anyway, thanks for picking that up and for the replacement pattern.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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