I have a worksheet full of data. Col A shows co_code. Each Co_code can have 1 or more rows of data. I would like to create a macro that adds and names a sheet in the same file, for each unique co_code, and then copy the applicable rows/information to the respective worksheet. Hope that makes sense.
I have code that creates a new workbook and then adds a sheet for each co_code. I dont want to create a new workbook, just add sheets within the existing workbook.
I'm trying to fix the code below, that almost does what I need it to, to create the new sheets within the current workbook and NOT in a new workbook.
Here is what I have:
Sub New_Worksheets_by_coCode()
Dim wbDest As Workbook
Dim rngFilter As Range, rngUniques As Range
Dim cell As Range, counter As Integer
Set rngFilter = Range("A1", Range("A" & Rows.Count).End(xlUp))
Application.ScreenUpdating = False
With rngFilter
.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Set rngUniques = Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
ActiveSheet.ShowAllData
End With
Application.SheetsInNewWorkbook = rngUniques.Count
Set wbDest = Workbooks.Add
Application.SheetsInNewWorkbook = 3
For Each cell In rngUniques
counter = counter + 1
rngFilter.AutoFilter Field:=1, Criteria1:=cell.Value
rngFilter.SpecialCells(xlCellTypeVisible).Copy Destination:=wbDest.Sheets(counter).Range("A1")
wbDest.Sheets(counter).Name = cell.Value
Next cell
rngFilter.Parent.AutoFilterMode = False
Application.ScreenUpdating = True
End Sub
I have code that creates a new workbook and then adds a sheet for each co_code. I dont want to create a new workbook, just add sheets within the existing workbook.
I'm trying to fix the code below, that almost does what I need it to, to create the new sheets within the current workbook and NOT in a new workbook.
Here is what I have:
Sub New_Worksheets_by_coCode()
Dim wbDest As Workbook
Dim rngFilter As Range, rngUniques As Range
Dim cell As Range, counter As Integer
Set rngFilter = Range("A1", Range("A" & Rows.Count).End(xlUp))
Application.ScreenUpdating = False
With rngFilter
.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Set rngUniques = Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
ActiveSheet.ShowAllData
End With
Application.SheetsInNewWorkbook = rngUniques.Count
Set wbDest = Workbooks.Add
Application.SheetsInNewWorkbook = 3
For Each cell In rngUniques
counter = counter + 1
rngFilter.AutoFilter Field:=1, Criteria1:=cell.Value
rngFilter.SpecialCells(xlCellTypeVisible).Copy Destination:=wbDest.Sheets(counter).Range("A1")
wbDest.Sheets(counter).Name = cell.Value
Next cell
rngFilter.Parent.AutoFilterMode = False
Application.ScreenUpdating = True
End Sub