Duplicate and rename a sheet based on a cell text

TS656577

New Member
Joined
Apr 7, 2015
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hello all, I'm looking for some help to duplicate a sheet and rename it based on a cell. I want to have any number of cells filled in with a name (picture shows 1,2,3,4,5,6 but it could be 15 or 20 cells with names) that after pressing a button it duplicates the "vendorblank" sheet and renames it based on the aforementioned cells.

I have this VBA code to create and rename sheets based on cell names, but i'm not sure where to incorporate the code to duplicate this sheet first. It also creates them in reverse order (6,5,4,3,2,1). Any help is greatly appreciated!

Sub CreateVendorSheet()

Dim rng As Range
Dim cell As Range

On Error GoTo Errorhandling

Set rng = Application.InputBox(Prompt:="Select cell range:", _
Title:="Create Vendor Sheet", _
Default:=Selection.Address, Type:=8)

For Each cell In rng

If cell <> "" Then

Sheets.Add.Name = cell
End If

Next cell

Errorhandling:

End Sub
 

Attachments

  • Capture3.PNG
    Capture3.PNG
    22.8 KB · Views: 5

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
try this
VBA Code:
Sub DuplicateAndRenameSheet()
    Dim ws As Worksheet
    Dim newName As String
    Dim i As Integer
    Dim rng As Range, cell As Range
    Dim vendorSheet As Worksheet

    Set vendorSheet = ThisWorkbook.Sheets("vendorblank")

    On Error Resume Next
    Set rng = Application.InputBox(Prompt:="Select cell range:", _
            Title:="Create Vendor Sheet", Default:=Selection.Address, Type:=8)
    On Error GoTo 0
    
    If rng Is Nothing Then
        MsgBox "No range selected. Operation canceled.", vbExclamation
        Exit Sub
    End If
    
    For Each cell In rng
        newName = cell.Value
        
        If newName <> "" Then
            vendorSheet.Copy After:=Sheets(Sheets.Count)

            ActiveSheet.Name = newName
        End If
    Next
End Sub
 
Upvote 1
Solution
try this
VBA Code:
Sub DuplicateAndRenameSheet()
    Dim ws As Worksheet
    Dim newName As String
    Dim i As Integer
    Dim rng As Range, cell As Range
    Dim vendorSheet As Worksheet

    Set vendorSheet = ThisWorkbook.Sheets("vendorblank")

    On Error Resume Next
    Set rng = Application.InputBox(Prompt:="Select cell range:", _
            Title:="Create Vendor Sheet", Default:=Selection.Address, Type:=8)
    On Error GoTo 0
   
    If rng Is Nothing Then
        MsgBox "No range selected. Operation canceled.", vbExclamation
        Exit Sub
    End If
   
    For Each cell In rng
        newName = cell.Value
       
        If newName <> "" Then
            vendorSheet.Copy After:=Sheets(Sheets.Count)

            ActiveSheet.Name = newName
        End If
    Next
End Sub
Absolute legend. Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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