Create sheets based on cell value

mmetzinger

Board Regular
Joined
Dec 30, 2010
Messages
61
I have the following code

Code:
'---------------------------------------------------------------------------------------------------------
'Create sheets for all agents in column E   V2.0
'--------------------------------------------------------------------------------------------------------
Dim FirstAgent As String
Dim LastAgent As String

    Columns("E:E").Select
    Selection.Find(What:="Agent Name", After:=ActiveCell, LookIn:=xlFormulas _
                , LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=True, SearchFormat:=False).Activate
    FirstAgent = ActiveCell.Row + 1
        
    Selection.Find(What:="Totals", After:=ActiveCell, LookIn:=xlFormulas, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=True, SearchFormat:=False).Activate
    LastAgent = ActiveCell.Row - 1
    
    'Make all the sheets for the agents
    Dim makesheets As Integer
    Do Until makesheets = LastAgent + 1
    Sheets.Add After:=Sheets(Sheets.Count)
    Loop

This part of the code works just fine. What I need now is to take the list of agent names that exist between "FirstAgent" and "LastAgent" and name each of the newly created sheets with that name.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Code:
Sub a()
'---------------------------------------------------------------------------------------------------------
'Create sheets for all agents in column E   V2.0
'--------------------------------------------------------------------------------------------------------
Dim FirstAgent As String, LastAgent As String

    Columns("E:E").Select
    Selection.Find(What:="Agent Name", After:=ActiveCell, LookIn:=xlFormulas _
                , LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=True, SearchFormat:=False).Activate
    FirstAgent = ActiveCell.Row + 1
        
    Selection.Find(What:="Totals", After:=ActiveCell, LookIn:=xlFormulas, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=True, SearchFormat:=False).Activate
    LastAgent = ActiveCell.Row - 1
    
    'Make all the sheets for the agents
    For arow = FirstAgent To LastAgent
      sheetname = Sheets(1).Cells(arow, 5).Value
      Sheets.Add After:=Sheets(Sheets.Count)
      ActiveSheet.Name = sheetname
    Next
End Sub
 
Upvote 0
Untest not sure if this helps.

Code:
Sub aBkFix()
    '---------------------------------------------------------------------------------------------------------
    'Create sheets for all agents in column E   V2.0
    '--------------------------------------------------------------------------------------------------------
    Dim FirstAgent As String, LastAgent As String
    Dim aStartTime
    
    'Speeding Up VBA Code
    With Application
        .ScreenUpdating = False 'Prevent screen flickering
        .Calculation = xlCalculationManual 'Preventing calculation
        .DisplayAlerts = False 'Turn OFF alerts
        .EnableEvents = False 'Prevent All Events
    End With
    
    'Start Timer
    aStartTime = Now()
    
    With Columns("E:E")
        .Find(What:="Agent Name", After:=ActiveCell, LookIn:=xlFormulas _
            , LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=True, SearchFormat:=False).Activate
        FirstAgent = ActiveCell.Row + 1
        
        .Find(What:="Totals", After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=True, SearchFormat:=False).Activate
        LastAgent = ActiveCell.Row - 1
        
    End With
    
    'Make all the sheets for the agents
    For arow = FirstAgent To LastAgent
        sheetname = Sheets(1).Cells(arow, 5).Value
        Sheets.Add After:=Sheets(Sheets.Count)
        ActiveSheet.Name = sheetname
    Next
    
    
    'Speeding Up VBA Code
    With Application
        .ScreenUpdating = True 'Prevent screen flickering
        .Calculation = xlAutomatic 'Preventing calculation
        .DisplayAlerts = True 'Turn OFF alerts
        .EnableEvents = True 'Prevent All Events
    End With
    
    'End Timer
    MsgBox "Time taken: " & Format(Now() - aStartTime, "h:mm:ss"), vbInformation, "Job Done!"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,759
Messages
6,126,730
Members
449,333
Latest member
Adiadidas

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