Copy Unique Values and Create sheet for each of them

Skjoll

New Member
Joined
Jul 5, 2018
Messages
3
Hi,

I'm currently new in recording basic macro. Can someone help me get the unique values for in the account column and create each account a separate worksheet?


What I currently have is just for 3 items to filter the account columns for each items, copy the contents, create worksheet for each item and paste the contents on the worksheets but I may encounter other accounts than what I currently recorded.

Code:
Sub Collapse()
'
' Collapse Macro
'


'
    ActiveSheet.ListObjects("Account").Range.AutoFilter Field:=3, Criteria1:= _
        "Account1"
    Range("Account[#All]").Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    ActiveSheet.ListObjects.Add(xlSrcRange, , xlYes).Name = _
        "Account1"
    Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).Formula = "=SUBTOTAL(109,_Account1[Amount])"
    Cells(Rows.Count, "A").End(xlUp).Value = "Total"
    ActiveSheet.Name = "Account1"
    Sheets("GL Transaction").Select
    ActiveSheet.ListObjects("Account").Range.AutoFilter Field:=3, Criteria1:= _
        "Account2"
    Range("Account[#All]").Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    ActiveSheet.ListObjects.Add(xlSrcRange, , xlYes).Name = _
        "Account2"
    Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).Formula = "=SUBTOTAL(109,_Account2[Amount])"
    Cells(Rows.Count, "A").End(xlUp).Value = "Total"
    ActiveSheet.Name = "Account2"
    Sheets("GL Transaction").Select
    ActiveSheet.ListObjects("Account").Range.AutoFilter Field:=3, Criteria1:= _
        "Account3"
    Range("Account[#All]").Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    ActiveSheet.ListObjects.Add(xlSrcRange, , xlYes).Name = _
        "Account3"
    Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).Formula = "=SUBTOTAL(109,_Account13[Amount])"
    Cells(Rows.Count, "A").End(xlUp).Value = "Total"
    ActiveSheet.Name = "Account13"
    Sheets("GL Transaction").Select
    ActiveSheet.ListObjects("Account").Range.AutoFilter Field:=3
    MsgBox ("Done!")
End Sub
Sorry for a messy code I am just a beginner with no training/basic knowledge or whatsoever.

Thank you very much!
 
Last edited by a moderator:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi & welcome to MrExcel.
Does this do what you want?
Code:
Sub Collapse()
   Dim Cl As Range
   Dim ws As Worksheet
   
   Set ws = Sheets("GL Transaction")
   If ws.FilterMode Then ws.ShowAllData
   With CreateObject("scripting.dictionary")
      For Each Cl In ws.ListObjects("Account").ListColumns(3).DataBodyRange
         If Not .exists(Cl.Value) Then
            Sheets.Add(, ws).name = Cl.Value
            .Add Cl.Value, Nothing
            ws.ListObjects("account").Range.autofilter 3, Cl.Value
            ws.ListObjects("account").Range.Copy Sheets(Cl.Value).Range("A1")
            Sheets(Cl.Value).ListObjects.Add.name = Cl.Value
         End If
      Next Cl
   End With
   ws.ShowAllData
End Sub
 
Upvote 0
Hi Fluff,

Yes that is very close to what I want. I wanted to understand this code so I may be able to revise it to my liking.
Thank you very much for your support! :)
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
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