macro for dividing master sheet to new sheets based on column

tsurvey

New Member
Joined
Jun 29, 2017
Messages
3
This is for Excel 2010. I have a workbook with the first tab "Master". I want to keep this tab for data entry but have separate tabs based on column G - "OwnerType" as the category, my first row is a header. I found a macro that looks like it might work for most of what I do but it does not appear to create the header row and it gives me a run error at:
If ws Is Nothing Then
Worksheets.Add(After:=Sheets(Sheets.Count)).Name = category.Value
Sheets("Master").Rows(1).Copy Cells(1, 1)

I am not a VBA programmer and only have limited experience in other languages so any help is appreciated. The steps I want to accomplish:
  1. find the first unique category, if a sheet does not exist, create one, add the header row (row 1), and then add all of the rows that also have that category.
  2. repeat for the rest of the table
  3. if a category already has an existing sheet, check each row to see if it has already been entered. Column A has a unique ID number (can be considered a primary key) that can be used for checking duplicates.
  4. Add any new rows to the appropriate sheet.

The Master sheet will be updated regularly so this is something that will be run from time to time. Here is the macro I copied from this site.
Sub AddSheet()
Application.ScreenUpdating = False
Dim bottomG As Long
bottomG = Sheets("Master").Range("G" & Rows.Count).End(xlUp).Row
Dim category As Range
Dim ws As Worksheet
Sheets("Master").Range("G2:G" & bottomG).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range _
("G2:G" & bottomG), Unique:=True
Set rnguniques = Sheets("Master").Range("G3:G" & bottomG).SpecialCells(xlCellTypeVisible)
If Sheets("Master").FilterMode Then Sheets("Master").ShowAllData
For Each category In rnguniques
Set ws = Nothing
On Error Resume Next
Set ws = Worksheets(category.Value)
On Error GoTo 0
If ws Is Nothing Then
Worksheets.Add(After:=Sheets(Sheets.Count)).Name = category.Value
Sheets("Master").Rows(1).Copy Cells(1, 1)
End If
Next category
For Each category In rnguniques
Sheets("Master").Range("G2:G" & bottomG).AutoFilter Field:=1, Criteria1:=category
Sheets("Master").Range("G3:G" & bottomG).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets(category.Value).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
If Sheets("Master").FilterMode Then Sheets("Master").ShowAllData
Next category
Application.ScreenUpdating = True
End Sub


Any help is greatly appreciated.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
See if this helps

Code:
Sub AddSheet()
 Application.ScreenUpdating = False
 Dim bottomG As Long
 bottomG = Sheets("Master").Range("G" & Rows.Count).End(xlUp).Row
 Dim category As Range
 Dim ws As Worksheet
 Sheets("Master").Range("G2:G" & bottomG).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range _
 ("G2:G" & bottomG), Unique:=True
 Set rnguniques = Sheets("Master").Range("G3:G" & bottomG).SpecialCells(xlCellTypeVisible)
 If Sheets("Master").FilterMode Then Sheets("Master").ShowAllData
     For Each category In rnguniques
         On Error Resume Next
         Set ws = Worksheets(category.Value)
         If Err.Number = 9 Then
            Worksheets.Add(After:=Sheets(Sheets.Count)).Name = category.Value
            Sheets("Master").Rows(1).Copy ActiveSheet.Cells(1, 1)
        End If
        On Error GoTo 0 
        Sheets("Master").Range("G2:G" & bottomG).AutoFilter Field:=1, Criteria1:=category
        Sheets("Master").Range("G3:G" & bottomG).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets(category.Value).Cells(Rows.Count, "A").End(xlUp).Offset  (1, 0)
        If Sheets("Master").FilterMode Then Sheets("Master").ShowAllData
      Next category
 Application.ScreenUpdating = True
 End Sub
 
Upvote 0
Thanks for the quick response. When I run the macro I get a Run-time error '9': subscript out of range
The debug highlights it at:
Sheets("Master").Range("G3:G" & bottomG).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets(category.Value).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
 
Upvote 0
dmt32 - That did the magic, AWESOME. I just got brownie points from the person trying to manage one of our projects. Wish I could share the beer with you:-) I really like the prompt feature, nice. Is there a way to mark this as the answer?
 
Upvote 0
dmt32 - That did the magic, AWESOME. I just got brownie points from the person trying to manage one of our projects. Wish I could share the beer with you:-) I really like the prompt feature, nice. Is there a way to mark this as the answer?

Hi,
I have seen many requests similar to yours so I developed that code - seems to work for most users & glad helped you with your requirement.
Never know, may get to bump in to each other one day for that beer.

Appreciate the feedback


Dave
 
Upvote 0

Forum statistics

Threads
1,224,759
Messages
6,180,813
Members
452,996
Latest member
nelsonsix66

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