Urgent - Excel/VBa help - identify text in cell and sort

nt_beans

New Member
Joined
Nov 10, 2015
Messages
26
All,

My original spreadhsheet with user name, location and package is in this format:
[TABLE="width: 500"]
<tbody>[TR]
[TD]First[/TD]
[TD]Last[/TD]
[TD]Location[/TD]
[TD]Package[/TD]
[/TR]
[TR]
[TD]Andrew[/TD]
[TD]Wan[/TD]
[TD]Chicago[/TD]
[TD][TABLE="width: 350"]
<tbody>[TR]
[TD="class: xl65, width: 350"]EX - RT CBOT, EXD - DELCOMEX, EXD - DEL NYMEX, NTWK WB BASE - QUOTES AG/COMMODITY, PUBLIC DEFAULTS[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD]Nicole[/TD]
[TD]Andy[/TD]
[TD]New York[/TD]
[TD][TABLE="width: 350"]
<tbody>[TR]
[TD="class: xl65, width: 350"]EX - RT CBOT, EX - RT CME, EX - RT WINNIPEG, EXD - DEL COMEX, EXD - DEL NYMEX, NTWK WB BASE - QUOTES EN, PUBLIC DEFAULTS[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD]Ram[/TD]
[TD]Sinha[/TD]
[TD]Seattle[/TD]
[TD][TABLE="width: 350"]
<tbody>[TR]
[TD="class: xl65, width: 350"]CASH MARKETS EDITION (PXCSH), , EX - RT CME, EX - RT EURONEXT ENCOM, EX - RT ICE (FORMERLY IPE), EX - RT ICE LIFFE, EX - RT MALAYSIA CPO, EX - RT NYBOT, EX - RT NYMEX, , LDP REPORTS (NPLDP), NTWK PRO BASE - AG/COMM EXD, [/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]

I want to format the above spreadsheet so if Col - Package contain either words CBOT or COMEX or NYMEX or CME then i want a new sheet with following information

[TABLE="width: 500"]
<tbody>[TR]
[TD]First[/TD]
[TD]Last[/TD]
[TD]Location[/TD]
[TD]Package[/TD]
[/TR]
[TR]
[TD]Andrew[/TD]
[TD]Wan[/TD]
[TD]Chicago[/TD]
[TD]CBOT[/TD]
[/TR]
[TR]
[TD]Andrew[/TD]
[TD]Wan[/TD]
[TD]Chicago[/TD]
[TD]COMEX[/TD]
[/TR]
[TR]
[TD]Andrew[/TD]
[TD]Wan[/TD]
[TD]Chicago[/TD]
[TD]NYMEX[/TD]
[/TR]
[TR]
[TD]Nicole[/TD]
[TD]Andy[/TD]
[TD]New York[/TD]
[TD]CBOT[/TD]
[/TR]
[TR]
[TD]Nicole[/TD]
[TD]Andy[/TD]
[TD]New York[/TD]
[TD]CME[/TD]
[/TR]
[TR]
[TD]Nicole[/TD]
[TD]Andy[/TD]
[TD]New York[/TD]
[TD]Comex[/TD]
[/TR]
[TR]
[TD]Nicole[/TD]
[TD]Andy[/TD]
[TD]New York[/TD]
[TD]NYMEX[/TD]
[/TR]
[TR]
[TD]Ram[/TD]
[TD]Sinha[/TD]
[TD]Seattle[/TD]
[TD]CME[/TD]
[/TR]
[TR]
[TD]Ram[/TD]
[TD]Sinha[/TD]
[TD]Seattle[/TD]
[TD]NYMEX[/TD]
[/TR]
</tbody>[/TABLE]


Thanks in advance for helping in this project
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
All,

My original spreadhsheet with user name, location and package is in this format:
[TABLE="width: 500"]
<tbody>[TR]
[TD]First[/TD]
[TD]Last[/TD]
[TD]Location[/TD]
[TD]Package[/TD]
[/TR]
[TR]
[TD]Andrew[/TD]
[TD]Wan[/TD]
[TD]Chicago[/TD]
[TD][TABLE="width: 350"]
<tbody>[TR]
[TD="class: xl65, width: 350"]EX - RT CBOT, EXD - DELCOMEX, EXD - DEL NYMEX, NTWK WB BASE - QUOTES AG/COMMODITY, PUBLIC DEFAULTS[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Is the above Package text correct as shown... there is no space between the "DEL" and the "COMEX"?
 
Upvote 0
There may be a more elegant way but this seems to work. Results on Sheet2.
Code:
Sub SplitCell()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim i As Long
    Dim splitPkg As Variant
    Dim pkg As Range
    For Each pkg In Range("D2:D" & LastRow)
        splitPkg = Split(pkg, ",")
        For i = LBound(splitPkg) To UBound(splitPkg)
            If splitPkg(i) Like "*CBOT*" Then
                Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = pkg.Offset(0, -3)
                Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = pkg.Offset(0, -2)
                Sheets("Sheet2").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = pkg.Offset(0, -1)
                Sheets("Sheet2").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0) = "CBOT"
            ElseIf splitPkg(i) Like "*COMEX*" Then
                Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = pkg.Offset(0, -3)
                Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = pkg.Offset(0, -2)
                Sheets("Sheet2").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = pkg.Offset(0, -1)
                Sheets("Sheet2").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0) = "COMEX"
            ElseIf splitPkg(i) Like "*NYMEX*" Then
                Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = pkg.Offset(0, -3)
                Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = pkg.Offset(0, -2)
                Sheets("Sheet2").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = pkg.Offset(0, -1)
                Sheets("Sheet2").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0) = "NYMEX"
            ElseIf splitPkg(i) Like "*CME*" Then
                Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = pkg.Offset(0, -3)
                Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = pkg.Offset(0, -2)
                Sheets("Sheet2").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = pkg.Offset(0, -1)
                Sheets("Sheet2").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0) = "CME"
            End If
        Next i
    Next pkg
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
My apologies there is indeed a space between "DEL" and "COMEX" .
Another question... is there a blank row between your data (you example looks like there is a blank cell between each Package cell)?
 
Last edited:
Upvote 0
Here is another macro that you can consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub ReformatDataLayout()
  Dim R As Long, X As Long, PackageCount As Long, Package As Variant, Data As Variant, Result As Variant
  PackageCount = Evaluate("SUM((LEN(D1:D4)-LEN(SUBSTITUTE(SUBSTITUTE(D1:D4,CHAR(160),"" ""),{""CME"",""NYMEX"",""CBOT"",""COMEX""},"""")))/{3,5,4,5})")
  Data = Range("A1").CurrentRegion
  ReDim Result(1 To PackageCount, 1 To 4)
  For R = 2 To UBound(Data)
    For Each Package In Array("CME", "CBOT", "COMEX", "NYMEX")
      If InStr(Data(R, 4), Package) Then
        X = X + 1
        Result(X, 1) = Data(R, 1)
        Result(X, 2) = Data(R, 2)
        Result(X, 3) = Data(R, 3)
        Result(X, 4) = Package
      End If
    Next
  Next
  Sheets("Sheet2").Range("A1:D1").Value = Range("A1:D1").Value
  Sheets("Sheet2").Range("A2").Resize(UBound(Result), 4) = Result
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Here is another macro that you can consider...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub ReformatDataLayout()
  Dim R As Long, X As Long, PackageCount As Long, Package As Variant, Data As Variant, Result As Variant
  PackageCount = Evaluate("SUM((LEN(D1:D4)-LEN(SUBSTITUTE(SUBSTITUTE(D1:D4,CHAR(160),"" ""),{""CME"",""NYMEX"",""CBOT"",""COMEX""},"""")))/{3,5,4,5})")
  Data = Range("A1").CurrentRegion
  ReDim Result(1 To PackageCount, 1 To 4)
  For R = 2 To UBound(Data)
    For Each Package In Array("CME", "CBOT", "COMEX", "NYMEX")
      If InStr(Data(R, 4), Package) Then
        X = X + 1
        Result(X, 1) = Data(R, 1)
        Result(X, 2) = Data(R, 2)
        Result(X, 3) = Data(R, 3)
        Result(X, 4) = Package
      End If
    Next
  Next
  Sheets("Sheet2").Range("A1:D1").Value = Range("A1:D1").Value
  Sheets("Sheet2").Range("A2").Resize(UBound(Result), 4) = Result
End Sub[/TD]
[/TR]
</tbody>[/TABLE]

Many thanks Rick...it works...appreciate all your help
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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