Home Topics excel How to duplicate a sheet in Excel with VBA

How to duplicate a sheet in Excel with VBA

Apr 05, 2025 am 09:34 AM

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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 after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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 of this Website
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

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Excel formula to find top 3, 5, 10 values in column or row Excel formula to find top 3, 5, 10 values in column or row Apr 01, 2025 am 05:09 AM

This tutorial demonstrates how to efficiently locate the top N values within a dataset and retrieve associated data using Excel formulas. Whether you need the highest, lowest, or those meeting specific criteria, this guide provides solutions. Findi

Add a dropdown list to Outlook email template Add a dropdown list to Outlook email template Apr 01, 2025 am 05:13 AM

This tutorial shows you how to add dropdown lists to your Outlook email templates, including multiple selections and database population. While Outlook doesn't directly support dropdowns, this guide provides creative workarounds. Email templates sav

How to use Flash Fill in Excel with examples How to use Flash Fill in Excel with examples Apr 05, 2025 am 09:15 AM

This tutorial provides a comprehensive guide to Excel's Flash Fill feature, a powerful tool for automating data entry tasks. It covers various aspects, from its definition and location to advanced usage and troubleshooting. Understanding Excel's Fla

Regex to extract strings in Excel (one or all matches) Regex to extract strings in Excel (one or all matches) Mar 28, 2025 pm 12:19 PM

In this tutorial, you'll learn how to use regular expressions in Excel to find and extract substrings matching a given pattern. Microsoft Excel provides a number of functions to extract text from cells. Those functions can cope with most

How to add calendar to Outlook: shared, Internet calendar, iCal file How to add calendar to Outlook: shared, Internet calendar, iCal file Apr 03, 2025 am 09:06 AM

This article explains how to access and utilize shared calendars within the Outlook desktop application, including importing iCalendar files. Previously, we covered sharing your Outlook calendar. Now, let's explore how to view calendars shared with

FV function in Excel to calculate future value FV function in Excel to calculate future value Apr 01, 2025 am 04:57 AM

This tutorial explains how to use Excel's FV function to determine the future value of investments, encompassing both regular payments and lump-sum deposits. Effective financial planning hinges on understanding investment growth, and this guide prov

MEDIAN formula in Excel - practical examples MEDIAN formula in Excel - practical examples Apr 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

How to remove / split text and numbers in Excel cell How to remove / split text and numbers in Excel cell Apr 01, 2025 am 05:07 AM

This tutorial demonstrates several methods for separating text and numbers within Excel cells, utilizing both built-in functions and custom VBA functions. You'll learn how to extract numbers while removing text, isolate text while discarding numbers

See all articles