Creating sheets from rows on a "Master Roster" sheet with each new sheet being a copy of a template

juscuz419

Board Regular
Joined
Apr 18, 2023
Messages
57
Office Version
  1. 2019
Platform
  1. Windows
I have a workbook that has a Master Roster Sheet and a Template sheet among others. The Master Roster sheet can and does change frequently from event to event. The Template sheet has cells that are populated from the Master Roster Sheet. I currently manually create a new copy of the template sheet for each row that is not blank within a range on the Master Roster sheet and then go to each sheet and repair the cell reference back to the Master Roster row for that individual sheet.
What I would like to build is a macro button that can be selected to run when the Master Roster range is complete for each event that creates a new sheet that is a copy of the template sheet with the cell references on each sheet accurately pointing back to specific information for a row in the Master Roster sheet
 
Jim, this is fantastic. I just got into it today a little while ago and it does exactly what I needed. I'm a 70 year old guy who understands logic and am somewhat adept at Excel but not much experience with VB. Trying to fix and automate some things for consistency to a group that is even more "technology challenged" than I am. Do I just copy the above corrected code and paste it into the CreateSheets macro from what you sent earlier?
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I'm glad to hear that the code works for you! I enjoyed the challenge. I'm a 65 year old that is somewhat Excel and VBA proficient.

If I understand your question, yes delete the existing sub and paste the new code into the code module.

If you need more assistance use this same thread again so I get notified when you create a new message.
 
Upvote 0
Jim, a couple of the guys I am trying to help have asked if it would be possible to "concatenate" the Fields named "Initials" and "Team Number" and have the macro create the "tab" name as that concatenation. Something that would look like ABC_1234A. Both of the fields are already named in the macro you set up earlier. I believe that the code below is the last that you sent me.

Sub CreateSheets()

' Worksheet object will point to the worksheet being processed.
Dim wsNewSheet As Worksheet

' Count of occupied rows in the Master Roster worksheet.
Dim iOccupiedRows As Long

' Used to iterate/loop through names found.
Dim iRow As Long

' Get count of all workshets in the workbook.
Dim iExistingSheetsCount As Long

' This variable holds the initials for a person being processed.
Dim sInitials As String

' Determine how many rows in the Master Roster worksheet contain names.
' Look in the column for initials. The header cell in that column is
' named "Header_Initials".
With [MasterRoster].Range("Header_Initials").Offset(1)

Do While .Cells(iOccupiedRows).Offset(1) <> ""
iOccupiedRows = iOccupiedRows + 1
Loop

End With

' If there are rows with data in them in the Master Roster then process the names.
If iOccupiedRows > 0 _
Then

' Delete all copies of the Template worksheet. Calls sub named DeleteExistingSheets.
Call DeleteExistingSheets

iExistingSheetsCount = ThisWorkbook.Worksheets.Count

' Iterate/loop through all rows containing data needed to populate a copy of
' the template worksheet.
For iRow = 1 To iOccupiedRows

' Get initials for the row/copy of template being processed.
sInitials = [MasterRoster].Range("Header_Initials").Offset(iRow).Value

' Do not try to create a sheet with no initials.
If sInitials <> "" _
Then

' Make copy of the template sheet. Place it after the most recently
' added copy of the template worksheet. Used code name to refer to the
' template worksheet. It is now the ActiveSheet.
[Template].Copy After:=Sheets(iExistingSheetsCount - 1)

' Keep track of how many copies of the template worksheet have been made.
iExistingSheetsCount = iExistingSheetsCount + 1

' Put data into the newly created copy of the template sheet. It is the ActiveSheet.
With ActiveSheet

' Name the newly created copy of the template sheet based on initials.
.Name = sInitials

' Copy data for the current row into the newly created copy of the template worksheet.
.Range("FirstName").Value = [MasterRoster].Range("Header_FirstName").Offset(iRow).Value

.Range("LastName").Value = [MasterRoster].Range("Header_LastName").Offset(iRow).Value

.Range("MiddleName").Value = [MasterRoster].Range("Header_MiddleName").Offset(iRow).Value

.Range("Address").Value = [MasterRoster].Range("Header_Address").Offset(iRow).Value

.Range("City").Value = [MasterRoster].Range("Header_City").Offset(iRow).Value

.Range("State").Value = [MasterRoster].Range("Header_State").Offset(iRow).Value

.Range("ZipCode").Value = [MasterRoster].Range("Header_ZipCode").Offset(iRow).Value

.Range("PhoneNumber").Value = [MasterRoster].Range("Header_PhoneNumber").Offset(iRow).Value

.Range("EmailAddress").Value = [MasterRoster].Range("Header_EmailAddress").Offset(iRow).Value

.Range("Position").Value = [MasterRoster].Range("Header_Position").Offset(iRow).Value

.Range("TeamNumber").Value = [MasterRoster].Range("Header_TeamNumber").Offset(iRow).Value

.Range("TagNumber").Value = [MasterRoster].Range("Header_TagNumber").Offset(iRow).Value

End With

End If

Next iRow

End If

End Sub
 
Upvote 0
Hopefully this does what is needed.

VBA Code:
Sub CreateSheets()

'   Worksheet object will point to the worksheet being processed.
    Dim wsNewSheet As Worksheet
    
'   Count of occupied rows in the Master Roster worksheet.
    Dim iOccupiedRows As Long
    
'   Used to iterate/loop through names found.
    Dim iRow As Long
    
'   Get count of all workshets in the workbook.
    Dim iExistingSheetsCount As Long
    
'   This variable holds the initials for a person being processed.
    Dim sInitials As String

'   This variable holds the team number for a person being processed.
    Dim sTeamNumber As String

'   Determine how many rows in the Master Roster worksheet contain names.
'   Look in the column for initials. The header cell in that column is
'   named "Header_Initials".
    With [MasterRoster].Range("Header_Initials").Offset(1)
    
        Do While .Cells(iOccupiedRows).Offset(1) <> ""
           iOccupiedRows = iOccupiedRows + 1
        Loop

    End With
    
'   If there are rows with data in them in the Master Roster then process the names.
    If iOccupiedRows > 0 _
     Then
     
'       Delete all copies of the Template worksheet. Calls sub named DeleteExistingSheets.
        Call DeleteExistingSheets
        
        iExistingSheetsCount = ThisWorkbook.Worksheets.Count

'       Iterate/loop through all rows containing data needed to populate a copy of
'       the template worksheet.
        For iRow = 1 To iOccupiedRows
            
'           Get initials for the row/copy of template being processed.
            sInitials = [MasterRoster].Range("Header_Initials").Offset(iRow).Value
            
'           Get team number for the row/copy of template being processed.
            sTeamNumber = [MasterRoster].Range("Header_TeamNumber").Offset(iRow).Value
            
'           Do not try to create a sheet with no initials.
            If sInitials <> "" _
             Then
            
'               Make copy of the template sheet. Place it after the most recently
'               added copy of the template worksheet. Used code name to refer to the
'               template worksheet. It is now the ActiveSheet.
                [Template].Copy After:=Sheets(iExistingSheetsCount - 1)
        
'               Keep track of how many copies of the template worksheet have been made.
                iExistingSheetsCount = iExistingSheetsCount + 1
            
'               Put data into the newly created copy of the template sheet. It is the ActiveSheet.
                With ActiveSheet
        
'                   Name the newly created copy of the template sheet based on initials and team number.
                    .Name = sInitials & "_" & sTeamNumber
        
'                   Copy data for the current row into the newly created copy of the template worksheet.
                    .Range("FirstName").Value = [MasterRoster].Range("Header_FirstName").Offset(iRow).Value
        
                    .Range("LastName").Value = [MasterRoster].Range("Header_LastName").Offset(iRow).Value
        
                    .Range("MiddleName").Value = [MasterRoster].Range("Header_MiddleName").Offset(iRow).Value
        
                    .Range("Address").Value = [MasterRoster].Range("Header_Address").Offset(iRow).Value
        
                    .Range("City").Value = [MasterRoster].Range("Header_City").Offset(iRow).Value
        
                    .Range("State").Value = [MasterRoster].Range("Header_State").Offset(iRow).Value
        
                    .Range("ZipCode").Value = [MasterRoster].Range("Header_ZipCode").Offset(iRow).Value
        
                    .Range("PhoneNumber").Value = [MasterRoster].Range("Header_PhoneNumber").Offset(iRow).Value
        
                    .Range("EmailAddress").Value = [MasterRoster].Range("Header_EmailAddress").Offset(iRow).Value
        
                    .Range("Position").Value = [MasterRoster].Range("Header_Position").Offset(iRow).Value
        
                    .Range("TeamNumber").Value = [MasterRoster].Range("Header_TeamNumber").Offset(iRow).Value
        
                    .Range("TagNumber").Value = [MasterRoster].Range("Header_TagNumber").Offset(iRow).Value
        
                End With
            
            End If
        
        Next iRow
    
    End If

End Sub
 
Upvote 0
Once again you are right on. I am learning a great deal from you and I appreciate you more than you know. I'm sure something else will come up. Are you still OK with giving an old cogger a hand occasionally?
 
Upvote 0
Feel free to use this same thread to ask for assistance again. Otherwise I won't know that you are asking for help (offline discussions are not allowed).
 
Upvote 0
Careful what you ask for my friend.

I am trying to pull some more info into the Template using your macro. I added 5 rows to it.

.Range("NoneVehicle").Value = [MasterRoster].Range("Header_VehicleNone").Offset(iRow).Value

.Range("RentalVehicle").Value = [MasterRoster].Range("Header_VehicleRental").Offset(iRow).Value

.Range("PersonalVehicle").Value = [MasterRoster].Range("Header_VehiclePersaonal").Offset(iRow).Value

.Range("_2WDVehicle").Value = [MasterRoster].Range("Header_Vehicle2WD").Offset(iRow).Value

.Range("_4WDAWDVehicle").Value = [MasterRoster].Range("Header_Vehicle4WDAWD").Offset(iRow).Value

I named the header fields as you had done. I named the places in the template and made sure that the cell references are to the right places in the template.
What happens is the macro creates the first sheet and then stops. When I go to debug it, it is stopping on the first of the rows I added. Cant figure it out because it created a correct sheet with the values from the Master Roster spreadsheet for the 5 fields filled in accurately.

Not sure if it has anything to do with it, but the values for all 5 fields will always be true or false.
 
Upvote 0
Are you willing to post the workbook? I'm not sure what the issue is without doing some testing. Your code looks correct from here. AFAIK the True/False thing should not be an issue. Di the True/False values transfer to the one sheet created?
 
Upvote 0
The workbook is pretty big and has a ton of proprietary and personal information in it. I'll make a dummy version tomorrow and see if I can get it to you. The True/False values did transfer to the one sheet that was created. The five fields are the linked fields of checkboxes on the template sheet. The cells that I named and point to with the lines I added are located in a section of each page that I use for various calculations.

Overall my effort here was to place the vehicle information on the master roster with some constraints that kept the users from making mistakes in the checkboxes on each sheet.
Basically, they can either have no vehicle, a rental vehicle, or a personal vehicle, but only one can be true. If they have no vehicle then the 2WD or 4WD/AWD checkboxes are both false. If they have a rental or personal vehicle they have either a 2WD or 4WD/AWD but not both.

Things would be a lot easier if the same checkbox could exist in multiple places.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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