search
HomeTopicsexcelHow to duplicate a sheet in Excel with VBA

This tutorial offers a suite of Excel macros for efficient sheet duplication: copying and renaming based on cell values, copying multiple sheets simultaneously, transferring active worksheets to other files without opening them, and more. Manually copying sheets is simple enough for a few sheets, but becomes tedious when dealing with numerous sheets repeatedly. These macros automate the process.

  • Copy sheet to a new workbook
  • Duplicate multiple sheets
  • Copy sheet to another Excel file
  • Copy and rename a sheet
  • Copy and rename a sheet based on cell value
  • Copy a worksheet to a closed workbook
  • Copy a sheet from another workbook without opening it
  • Duplicate a sheet multiple times
  • Copying sheets in Excel using VBA

Excel VBA Macro: Copying a Sheet to a New Workbook

This concise macro copies the active sheet to a new workbook.

Public Sub CopySheetToNewWorkbook()
    ActiveSheet.Copy
End Sub

Excel VBA Macro: Copying Multiple Sheets

Select the desired worksheets and run this macro to copy them to a new workbook.

Public Sub CopySelectedSheets()
    ActiveWindow.SelectedSheets.Copy
End Sub

Excel VBA Macro: Copying a Sheet to Another Workbook

These macros copy the active sheet to another workbook, offering options for placement:

Copying to the beginning of another workbook: This macro inserts the copied sheet before the first sheet in the destination workbook ("Book1.xlsx" – replace with your file path).

Public Sub CopySheetToBeginningAnotherWorkbook()
    ActiveSheet.Copy Before:=Workbooks("Book1.xlsx").Sheets(1)
End Sub

Copying to the end of another workbook: This macro appends the copied sheet to the end of the destination workbook ("Book1.xlsx" – replace with your file path).

Public Sub CopySheetToEndAnotherWorkbook()
    ActiveSheet.Copy After:=Workbooks("Book1.xlsx").Sheets(Workbooks("Book1.xlsx").Worksheets.Count)
End Sub

Note: The target workbook must exist.

Copying to a selected workbook: This utilizes a UserForm (UserForm1) with a ListBox (ListBox1) to select the destination workbook from open workbooks. Two buttons control selection and closing.

How to duplicate a sheet in Excel with VBA

The UserForm code:

Public SelectedWorkbook As String

Private Sub UserForm_Initialize()
    SelectedWorkbook = ""
    ListBox1.Clear
    For Each wbk In Application.Workbooks
        ListBox1.AddItem (wbk.Name)
    Next
End Sub

Private Sub CommandButton1_Click()
    If ListBox1.ListIndex > -1 Then
        SelectedWorkbook = ListBox1.List(ListBox1.ListIndex)
    End If
    Me.Hide
End Sub

Private Sub CommandButton2_Click()
    SelectedWorkbook = ""
    Me.Hide
End Sub

Macros to use with the UserForm:

Copy to the beginning of the selected workbook:

Public Sub CopySheetToBeginningSelectedWorkbook()
    Load UserForm1
    UserForm1.Show
    If (UserForm1.SelectedWorkbook  "") Then
        ActiveSheet.Copy Before:=Workbooks(UserForm1.SelectedWorkbook).Sheets(1)
    End If
    Unload UserForm1
End Sub

Copy to the end of the selected workbook:

Public Sub CopySheetToEndSelectedWorkbook()
    Load UserForm1
    UserForm1.Show
    If (UserForm1.SelectedWorkbook  "") Then
        ActiveSheet.Copy After:=Workbooks(UserForm1.SelectedWorkbook).Sheets(Workbooks(UserForm1.SelectedWorkbook).Worksheets.Count)
    End If
    Unload UserForm1
End Sub

The macro will display a list of open workbooks for selection.

How to duplicate a sheet in Excel with VBA

Excel Macro: Copying and Renaming a Sheet

These macros automate sheet renaming after copying:

This macro copies the active sheet, names it "Test Sheet" (customizable), and places it at the end.

Public Sub CopySheetAndRenamePredefined()
    ActiveSheet.Copy After:=Worksheets(Sheets.Count)
    On Error Resume Next
    ActiveSheet.Name = "Test Sheet"
End Sub

This macro prompts the user for a custom sheet name.

Public Sub CopySheetAndRename()
    Dim newName As String
    On Error Resume Next
    newName = InputBox("Enter the name for the copied worksheet")
    If newName  "" Then
        ActiveSheet.Copy After:=Worksheets(Sheets.Count)
        On Error Resume Next
        ActiveSheet.Name = newName
    End If
End Sub

The macro displays an input box for name entry.

How to duplicate a sheet in Excel with VBA

Excel Macro: Copying and Renaming Based on Cell Value

These macros rename the copied sheet using a cell's value:

This macro uses the currently selected cell's value for the new sheet name.

Public Sub CopySheetAndRenameByCell()
    Dim newName As String
    On Error Resume Next
    newName = InputBox("Enter the name for the copied worksheet", "Copy worksheet", ActiveCell.Value)
    If newName  "" Then
        ActiveSheet.Copy After:=Worksheets(Sheets.Count)
        On Error Resume Next
        ActiveSheet.Name = newName
    End If
End Sub

This macro uses the value of cell A1 (changeable) for the new sheet name.

Public Sub CopySheetAndRenameByCell2()
    Dim wks As Worksheet
    Set wks = ActiveSheet
    ActiveSheet.Copy After:=Worksheets(Sheets.Count)
    If wks.Range("A1").Value  "" Then
        On Error Resume Next
        ActiveSheet.Name = wks.Range("A1").Value
    End If
    wks.Activate
End Sub

How to duplicate a sheet in Excel with VBA

Excel Macro: Copying to a Closed Workbook

This macro copies the active sheet to a closed workbook selected via a file dialog.

How to duplicate a sheet in Excel with VBA

Public Sub CopySheetToClosedWorkbook()
    Dim fileName
    Dim closedBook As Workbook
    Dim currentSheet As Worksheet
    fileName = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx")
    If fileName  False Then
        Application.ScreenUpdating = False
        Set currentSheet = Application.ActiveSheet
        Set closedBook = Workbooks.Open(fileName)
        currentSheet.Copy After:=closedBook.Sheets(closedBook.Worksheets.Count)
        closedBook.Close (True)
        Application.ScreenUpdating = True
    End If
End Sub

Excel VBA Macro: Copying from a Closed Workbook

This macro copies a sheet from a specified closed workbook (update path and sheet name).

Public Sub CopySheetFromClosedWorkbook()
    Dim sourceBook As Workbook
    Application.ScreenUpdating = False
    Set sourceBook = Workbooks.Open("C:\\Users\\XXX\\Documents\\Target_Book.xlsx") 'Update path
    sourceBook.Sheets("Sheet1").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) 'Update sheet name
    sourceBook.Close
    Application.ScreenUpdating = True
End Sub

Excel VBA Macro: Duplicating a Sheet Multiple Times

This macro creates multiple copies of the active sheet.

Public Sub DuplicateSheetMultipleTimes()
    Dim n As Integer
    On Error Resume Next
    n = InputBox("How many copies of the active sheet do you want to make?")
    If n >= 1 Then
        For numtimes = 1 To n
            ActiveSheet.Copy After:=ActiveWorkbook.Sheets(Worksheets.Count)
        Next
    End If
End Sub

The macro displays an input box for the number of copies.

How to duplicate a sheet in Excel with VBA

Adding Macros to Your Workbook:

  1. Open the Excel workbook.
  2. Press Alt F11 to open the VBA editor.
  3. Right-click "ThisWorkbook," select "Insert" > "Module."
  4. Paste the macro code into the module.
  5. Press F5 to run.

Running Macros from a Sample Workbook: (Download a sample workbook containing these macros). Open the sample workbook, then in your own workbook, press Alt F8, select the macro, and click "Run."

How to duplicate a sheet in Excel with VBA

The above is the detailed content of How to duplicate a sheet in Excel with VBA. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MEDIAN formula in Excel - practical examplesMEDIAN formula in Excel - practical examplesApr 11, 2025 pm 12:08 PM

This tutorial explains how to calculate the median of numerical data in Excel using the MEDIAN function. The median, a key measure of central tendency, identifies the middle value in a dataset, offering a more robust representation of central tenden

Google Spreadsheet COUNTIF function with formula examplesGoogle Spreadsheet COUNTIF function with formula examplesApr 11, 2025 pm 12:03 PM

Master Google Sheets COUNTIF: A Comprehensive Guide This guide explores the versatile COUNTIF function in Google Sheets, demonstrating its applications beyond simple cell counting. We'll cover various scenarios, from exact and partial matches to han

Excel shared workbook: How to share Excel file for multiple usersExcel shared workbook: How to share Excel file for multiple usersApr 11, 2025 am 11:58 AM

This tutorial provides a comprehensive guide to sharing Excel workbooks, covering various methods, access control, and conflict resolution. Modern Excel versions (2010, 2013, 2016, and later) simplify collaborative editing, eliminating the need to m

How to convert Excel to JPG - save .xls or .xlsx as image fileHow to convert Excel to JPG - save .xls or .xlsx as image fileApr 11, 2025 am 11:31 AM

This tutorial explores various methods for converting .xls files to .jpg images, encompassing both built-in Windows tools and free online converters. Need to create a presentation, share spreadsheet data securely, or design a document? Converting yo

Excel names and named ranges: how to define and use in formulasExcel names and named ranges: how to define and use in formulasApr 11, 2025 am 11:13 AM

This tutorial clarifies the function of Excel names and demonstrates how to define names for cells, ranges, constants, or formulas. It also covers editing, filtering, and deleting defined names. Excel names, while incredibly useful, are often overlo

Standard deviation Excel: functions and formula examplesStandard deviation Excel: functions and formula examplesApr 11, 2025 am 11:01 AM

This tutorial clarifies the distinction between standard deviation and standard error of the mean, guiding you on the optimal Excel functions for standard deviation calculations. In descriptive statistics, the mean and standard deviation are intrinsi

Square root in Excel: SQRT function and other waysSquare root in Excel: SQRT function and other waysApr 11, 2025 am 10:34 AM

This Excel tutorial demonstrates how to calculate square roots and nth roots. Finding the square root is a common mathematical operation, and Excel offers several methods. Methods for Calculating Square Roots in Excel: Using the SQRT Function: The

Google Sheets basics: Learn how to work with Google SpreadsheetsGoogle Sheets basics: Learn how to work with Google SpreadsheetsApr 11, 2025 am 10:23 AM

Unlock the Power of Google Sheets: A Beginner's Guide This tutorial introduces the fundamentals of Google Sheets, a powerful and versatile alternative to MS Excel. Learn how to effortlessly manage spreadsheets, leverage key features, and collaborate

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment