Remove values from string based on criteria

JP_Romano

New Member
Joined
Nov 29, 2016
Messages
2
Good day all - struggling a bit with how to do this, and am hoping somebody here already has it worked out somewhere and would be willing to share it.

I have a series of strings that look, roughly like the sting below. There can be anywhere from 1 to 1000 elements. The string is in this order:
CODE|SCORE|DESCRIPTION

ASIA|(0) (100)|Asia, Pacific Rim|BIZNEWS|(100) (100)|Bus, Eco, Govt News|BUSINESS|(100) (100)|Business News|CECO|(100) (100)|Country Economies|EURX|(70) (100)|European Exchange Vals|

What I need to do, is remove all elements when the score is NOT (100)(100). So for the above, I should be left with the string below (the first and last set of element have been removed as they do not meet the criteria:

BIZNEWS|(100) (100)|Bus, Eco, Govt News|BUSINESS|(100) (100)|Business News|CECO|(100) (100)|Country Economies

I hope this is sufficient info - if not, and you're willing to lend a hand, please let me know what else I can provide.

Thank you!
JP
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Using VBA and assuming your data is in column A beginning in cell A1. Uses sheet2 for display.
Code:
Sub extract()
Dim sh As Worksheet, c As Range, spl As Variant, i As Long
Set sh = Sheets("Sheet1") 'edit sheet name
    For Each c In sh.Range("A1", sh.Cells(Rows.Count, 1).End(xlUp))
        spl = Split(c.Value, "|")
        For i = LBound(spl) To UBound(spl)
            If InStr(spl(i), "(100) (100)") > 0 Then
                txt = txt & spl(i - 1) & spl(i) & spl(i + 1) & "|"
            End If
        Next
        Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp)(2) = txt
    Next
End Sub
 
Upvote 0
Hello!

This is the best I could come up with:
Code:
Sub JP_Romano()

Dim lrow As Long
Dim lclm As Long
Dim maxlclm As Long
Dim i As Long
Dim j As Long
Dim counter As Integer

lrow = Cells(Rows.Count, 1).End(xlUp).Row
counter = 3
Range("A:A").TextToColumns Destination:=Range("A:A"), DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, OtherChar:="|"
Columns(1).Insert
maxlclm = 1

For i = 1 To lrow
    lclm = Cells(i, Columns.Count).End(xlToLeft).Column
    If lclm > maxlclm Then maxlclm = lclm
    For j = 3 To lclm
        If j = counter Then
            If Trim(Cells(i, j)) = "(100) (100)" Then Cells(i, 1) = Cells(i, 1) & Cells(i, j - 1) & "|" & Cells(i, j) & "|" & Cells(i, j + 1) & "|"
            counter = counter + 3
        End If
    Next j
    Cells(i, 1) = Left(Cells(i, 1), Len(Cells(i, 1)) - 1)
    counter = 3
Next i
Range(Columns(2), Columns(maxlclm)).Delete
End Sub

It requires no data to be in the columns right of your data. It also assumes your list of strings are in column A.

It seems to be working great in my test environment. Let me know how it does!

Sincerely,
Max
 
Upvote 0
Thank you both so much - these are both brilliant.... can't wait to try them out in the morning (sadly, I forgot to bring home the biometric device that lets me log in remotely).

Really, really appreciate your time and help.
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

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