Macro to sort data in an Array

porterrm

New Member
Joined
Aug 21, 2018
Messages
5
Hi

I am looking to sorta data set that is about 10,000 rows by 6 columns and I need help writing asimple macro

I made an example to illustrate what I am looking for.

I want to search the first column of data in Table 1and look for all rows that contain the letter "a" and put all of those rows in a new Table 2. Then it does the same thing for rows that contain the letter "b" and then "c". In the end I would have three separate tables with the data sorted.

Table 1Table 2Table 3Table 4
XyZXyZXyZXyZ
a114a114b123c132
b123a214b223c232
c132
a214
b223
c232

<tbody>
</tbody>


I did somethingsimilar to this years ago with arrays. Put all the data from the original tablein an Array and the with an if statement loop through the array and then it populated "a" array, "b" array and a "c" array. Once thatwas done it printed them out to designated cells.

If anyone has anyrecommendations or can point me to a thread I missed(I looked but am relativelynew to the site) I would appreciate it.

Thank you and I appreciate the help

Porter
 
Last edited by a moderator:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Start:


Book1
ABC
1XyZ
2a114
3b123
4c132
5a214
6b223
7c232
Sheet1


Macro:

Code:
Public Sub SplitTable()

Dim lastRow As Long
Dim thisRow As Long
Dim nextRow(2) As Long
Dim tableNum As Long

lastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range(Range("D1"), Cells(lastRow, "O")).ClearContents
For tableNum = 0 To 2
    nextRow(tableNum) = 2
    Range(Cells(1, tableNum * 4 + 5), Cells(1, tableNum * 4 + 7)).Value = Range("A1:C1").Value
Next tableNum

For thisRow = 2 To lastRow
    Select Case Left(Cells(thisRow, "A").Value, 1)
        Case "a"
            tableNum = 0
        Case "b"
            tableNum = 1
        Case "c"
            tableNum = 2
        Case Else
            tableNum = 4
    End Select
    
    If tableNum < 4 Then
        Range(Cells(nextRow(tableNum), tableNum * 4 + 5), Cells(nextRow(tableNum), tableNum * 4 + 7)).Value = Range("A" & thisRow & ":C" & thisRow).Value
        nextRow(tableNum) = nextRow(tableNum) + 1
    End If
Next thisRow

End Sub

End:


Book1
ABCDEFGHIJKLMNO
1XyZXyZXyZXyZ
2a114a114b123c132
3b123a214b223c232
4c132
5a214
6b223
7c232
Sheet1


WBD
 
Upvote 0
Another option:- Data as per your Thread, actual data starts row 2.
Results every forth column
Code:
[COLOR="Navy"]Sub[/COLOR] MG22Aug05
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, t [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] col [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ray() [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Fd [COLOR="Navy"]As[/COLOR] Boolean
[COLOR="Navy"]Set[/COLOR] Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] n = 97 To 122
c = 2: Fd = False
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
       
        [COLOR="Navy"]If[/COLOR] Left(Dn.Value, 1) = Chr(n) [COLOR="Navy"]Then[/COLOR]
            Fd = True
            c = c + 1
            ReDim Preserve Ray(1 To 3, 1 To c)
            Ray(2, 1) = "Table "
            Ray(1, c) = Dn.Value
            Ray(2, c) = Dn.Offset(, 1).Value
            Ray(3, c) = Dn.Offset(, 2).Value
        [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
    [COLOR="Navy"]If[/COLOR] Fd [COLOR="Navy"]Then[/COLOR]
        t = t + 1
        col = col + 4
        Ray(2, 1) = "Table " & t
        Ray(1, 2) = Rng(1)
        Ray(2, 2) = Rng(1).Offset(, 1).Value
        Ray(3, 2) = Rng(1).Offset(, 1).Value
        [COLOR="Navy"]With[/COLOR] Cells(1, col + 1).Resize(c, 3)
            .Value = Application.Transpose(Ray)
            .HorizontalAlignment = xlCenter
        [COLOR="Navy"]End[/COLOR] With
         Erase Ray
    
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Welcome to the MrExcel board!

.. yet another option to try
Code:
Sub Make_Tables()
  Dim itm As Variant
  
  For Each itm In Split("a b c")
    Range("ZZ2").Formula = "=search(""" & itm & """,A2)"
    Range("A1").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range("ZZ1:ZZ2"), CopyToRange:=Cells(1, Columns.Count).End(xlToLeft).Offset(, 2), Unique:=False
  Next itm
  Range("ZZ2").ClearContents
End Sub
 
Upvote 0
Hi Peter,
Hi Peter,

Thanks for the post. Would you mind providing some commentsfor your code? This seems like a really clean was of solving this problem but Iam having a hard time following it. Thanks!


Porter
 
Upvote 0
Would you mind providing some commentsfor your code?
Hmm, there isn't a lot of explanation to include, but here goes ..
Rich (BB code):
Sub Make_Tables()
  Dim itm As Variant
  
  'For each item that you want to search for in column A
  For Each itm In Split("a b c")
    'Put an Advanced Filter criteria formula in an empty column
    Range("ZZ2").Formula = "=search(""" & itm & """,A2)"
    'Do an Advanced Filter using that formula & writing the results offset to the right
    Range("A1").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range("ZZ1:ZZ2"), CopyToRange:=Cells(1, Columns.Count).End(xlToLeft).Offset(, 2), Unique:=False
  Next itm
  'Clear the Advanced Filter criteria formula
  Range("ZZ2").ClearContents
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,101
Members
449,066
Latest member
Andyg666

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