Create named worksheets

bobkap

Active Member
Joined
Nov 22, 2009
Messages
313
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
I have come to the point in my macro where I have many rows of unsorted data. Each row has a person's name. I need to create a worksheet for each person, name the worksheet their name and copy JUST their data to the sheet with their name. Data looks something like this:

Date Name1 Location1 [more columns of data]
Date2 Name2 Location2 [more columns of data]
Date Name1 Location1 [more columns of data]
Date Name1 Location1 [more columns of data]
Date3 Name2 Location3 [more columns of data]
Date Name3 Location4 [more columns of data]
etc etc

Any help our suggestions of how to handle with VBA would be greatly appreciated.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
This should work. (Error handling to be added)
Code:
Sub test()
    Dim oneCell As Range
    Dim DataRange As Range
    Dim i As Long
    With Sheets("Sheet1").Range("A:A")
        Set DataRange = Range(.Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp))
    End With
    
    For Each oneCell In DataRange
        With ThisWorkbook.Worksheets.Add
            .Rows(1).Value = oneCell.EntireRow.Value
            .Name = CStr(oneCell.Value)
        End With
    Next oneCell
End Sub
 
Upvote 0
I think the above missed some things. This is better.
Code:
Sub test2()
    Dim oneCell As Range
    Dim dataRange As Range
    Dim i As Long, sheetName As String
    With Sheets("Sheet1").Range("A:A")
        Set dataRange = Range(.Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp))
    End With
    
    For Each oneCell In dataRange
        sheetName = CStr(oneCell.Value)
        On Error Resume Next
        If LCase(ThisWorkbook.Sheets(sheetName).Name) <> LCase(sheetName) Then
            On Error GoTo 0
            With ThisWorkbook.Worksheets.Add
                .Rows(1).Value = oneCell.EntireRow.Value
                .Name = sheetName
            End With
        Else
            On Error GoTo 0
            ThisWorkbook.Sheets(sheetName).Range("A65536").End(xlUp).Offset(1, 0).EntireRow.Value = oneCell.EntireRow.Value
        End If
    Next oneCell
End Sub
 
Upvote 0
Awesome. Mega thanks! JUST when I think I know what I'm doing in VBA I get reminded that I have SOOO much more to learn.

Not sure if you got my reply to your message. Sorry if this is a repeat.

Thanks VERY much for your help! It works beautifully. Someday, I hope to be better educated in VBA so I can understand how to do things like this. :)

If I'm not pushing my luck, may I ask for your help to figure out the following please?:
1. My headers are in row 1 of course. How do I avoid creating a new sheet with just these headers in it?
2. How do I have the first 13 of these headers show up in row 1 of each of these new sheets?

Thanks again!
 
Upvote 0
1) Start the definition of dataRange on row 2
Code:
Set dataRange = Range(.Cells(1, [COLOR="#FF0000"]2[/COLOR]), .Cells(Rows.Count, 1).End(xlUp))

2) Not sure if this is what you mean, but if you want A1:M1 of each of the new sheets to be the same as A1:M1 of Sheet1:

Code:
Sub test3()
    Dim oneCell As Range
    Dim dataRange As Range
    Dim i As Long, sheetName As String
    Dim arrHeaders as Variant
    With Sheets("Sheet1").Range("A:A")
        Set dataRange = Range(.Cells(1, [COLOR="#FF0000"]2[/COLOR]), .Cells(Rows.Count, 1).End(xlUp))
        [COLOR="#FF0000"]arrHeaders = .Resize(1, 13).Value[/COLOR]
    End With
    
    For Each oneCell In dataRange
        sheetName = CStr(oneCell.Value)
        On Error Resume Next
        If LCase(ThisWorkbook.Sheets(sheetName).Name) <> LCase(sheetName) Then
            On Error GoTo 0
            With ThisWorkbook.Worksheets.Add
                [COLOR="#FF0000"].Cells(1,1).Resize(1, 13).Value = arrHeaders[/COLOR]
                .Rows([COLOR="#FF0000"]2[/COLOR]).Value = oneCell.EntireRow.Value
                .Name = sheetName
            End With
        Else
            On Error GoTo 0
            ThisWorkbook.Sheets(sheetName).Range("A65536").End(xlUp).Offset(1, 0).EntireRow.Value = oneCell.EntireRow.Value
        End If
    Next oneCell
End Sub
 
Upvote 0
Thanks again! Everything works perfectly now except it still creates a worksheet using the name of my header cell(1,1) contents.
 
Upvote 0
Try this change
Code:
Set dataRange = Range(.Cells([COLOR="#FF0000"]2, 1[/COLOR]), .Cells(Rows.Count, 1).End(xlUp))
 
Upvote 0

Forum statistics

Threads
1,214,598
Messages
6,120,441
Members
448,966
Latest member
DannyC96

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