Move rows to new sheets based on 50 unique cell values

ariffin

New Member
Joined
Dec 15, 2019
Messages
6
Office Version
  1. 2013
Platform
  1. Windows
Hi all,

i am currently working on a project where i have a large list of exactly 6030 unique names under the title "Module Name" located under Column A.
I have categorized each row with a name under Column B - total of 51 categories.

i would like to run a macro that would create New sheets based on these 51 categories as well as move or copy the rows to the appropriate sheet.

i am a beginner in terms of macros and i did try to research on the forums for a macro similar to this. i found one where based on the input of a cell value, the row moves the sheet, but the problem is, i have already input this cell value, and to input 6020 rows individually will take a large time.

Any solution is greatly appreciated.
 

Attachments

  • excel.PNG
    excel.PNG
    61.9 KB · Views: 14

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Welcome to the MrExcel board!

Assuming that the sheet you have shown is the left hand (or only) sheet in the workbook, give this macro a try in a copy of that workbook.

VBA Code:
Sub CreateDataSheets()
  Dim d As Object
  Dim a As Variant, itm As Variant
 
  Set d = CreateObject("Scripting.Dictionary")
  With Sheets(1)
    a = .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).Value
    For Each itm In a
      d(itm) = Empty
    Next itm
    Application.ScreenUpdating = False
    For Each itm In d.Keys()
      .Copy After:=Sheets(Sheets.Count)
      With Sheets(Sheets.Count).UsedRange
        .AutoFilter Field:=2, Criteria1:="<>" & itm
        .Offset(1).EntireRow.Delete
        .Parent.AutoFilterMode = False
        .Parent.Name = itm
      End With
    Next itm
    Application.ScreenUpdating = True
  End With
End Sub
 
Upvote 0
wow, that worked amazing. there was an error, but i think it is because i had a special character in some of my category name. i will amend the category name and try the code again.
thanks so much, you saved me hours of work.
 
Upvote 0
Check it out.
VBA Code:
Sub Make_Sheets()
    Dim cUnique As Collection
    Dim rng As Range
    Dim c As Range
    Dim sh As Worksheet, WS As Worksheet
    Dim vNum As Variant
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    Set sh = ThisWorkbook.Sheets("Sheet1")
    For Each Sheet In Sheets
        If Sheet.Name <> sh.Name Then Sheet.Delete
    Next Sheet
    
    With sh
        Set rng = .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)
        Set cUnique = New Collection
        On Error Resume Next
        For Each c In rng.Cells
            cUnique.Add c.Value, CStr(c.Value)
        Next c
        On Error GoTo 0

        For Each vNum In cUnique
            Set WS = ThisWorkbook.Sheets.Add(After:= _
                                             ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))

            WS.Name = vNum
            .Range("A1").AutoFilter Field:=2, Criteria1:=vNum
            rng.Resize(rng.Rows.Count + 1, rng.Columns.Count + 1).Offset(-1, -1).SpecialCells(xlCellTypeVisible).Copy WS.Range("A1")
            .Range("A1").AutoFilter
        Next vNum

    End With
    sh.Select
End Sub
 
Upvote 0
wow, that worked amazing. there was an error, but i think it is because i had a special character in some of my category name. i will amend the category name and try the code again.
thanks so much, you saved me hours of work.
You're welcome. :)

Yes, special characters could cause a problem, particularly with the naming of the new worksheets.
Let us know if you need anything further.
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,543
Members
449,316
Latest member
sravya

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