I've been working on a workbook to create and populate sheets based on values in a pivot table.
I create sheets based on the list using something similar to this (credit to rizvisa1 on ccm.net):
Sub CreateSheetsFromAList()
Dim nameSource As String 'sheet name where to read names
Dim nameColumn As String 'column where the names are located
Dim nameStartRow As Long 'row from where name starts
Dim detailSheet As String 'sales detail sheet name
Dim detailRange As String 'range to copy from sales detail sheet
Dim nameEndRow As Long 'row where name ends
Dim employeeName As String 'employee name
Dim newSheet As Worksheet
nameSource = "Pivot"
nameColumn = "A"
nameStartRow = 5
detailSheet = "Pivot"
'this is the range where I want to only copy and paste the rows/records that match the new sheet name
detailRange = "A5:D463"
'find the last cell in use
nameEndRow = Sheets(nameSource).Cells(Rows.Count, nameColumn).End(xlUp).Row
'loop till last row
Do While (nameStartRow <= nameEndRow)
'get the name
employeeName = Sheets(nameSource).Cells(nameStartRow, nameColumn)
'remove any white space
employeeName = Trim(employeeName)
' if name is not equal to ""
If (employeeName <> vbNullString) Then
On Error Resume Next 'do not throw error
Err.Clear 'clear any existing error
'if sheet name is not present this will cause error to leverage
Sheets(employeeName).Name = employeeName
If (Err.Number > 0) Then
'sheet was not there, so it create error, so we can create this sheet
Err.Clear
On Error GoTo -1 'disable exception so to reuse in loop
'add new sheet
Set newSheet = Sheets.Add(After:=Sheets(Sheets.Count))
'rename sheet
newSheet.Name = employeeName
Application.CutCopyMode = False 'clear clipboard
'copy sales detail
Sheets(detailSheet).Range(detailRange).Copy
'paste training material
Sheets(employeeName).Cells(1, "A").PasteSpecial
Application.CutCopyMode = False
End If
End If
nameStartRow = nameStartRow + 1 'increment row
Loop
End Sub
I've only been copying a static range.
My issue is selecting the range where the first column matches the sheet name in order to copy and paste into the newly created sheet. I've tried using For Each
where a cell matches the sheet name and copying the entire row.
Here's what I'm trying to do:
Take a sheet with the following data in a pivot table:
And turn it into new sheets with the sheet names from column A, populated with only the data that matches the sheet name like this: