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.
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.
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.
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
Excel Macro: Copying to a Closed Workbook
This macro copies the active sheet to a closed workbook selected via a file dialog.
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.
Adding Macros to Your Workbook:
- Open the Excel workbook.
- Press Alt F11 to open the VBA editor.
- Right-click "ThisWorkbook," select "Insert" > "Module."
- Paste the macro code into the module.
- 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."
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!

This article will guide you through the process of creating a timeline for Excel pivot tables and charts and demonstrate how you can use it to interact with your data in a dynamic and engaging way. You've got your data organized in a pivo

This article explains how to create drop-down lists in Excel using data validation, including single and dependent lists. It details the process, offers solutions for common scenarios, and discusses limitations such as data entry restrictions and pe

The article discusses methods to sum columns in Excel using the SUM function, AutoSum feature, and how to sum specific cells.

The article details steps to create and customize pie charts in Excel, focusing on data preparation, chart insertion, and personalization options for enhanced visual analysis.

Article discusses creating, formatting, and customizing tables in Excel, and using functions like SUM, AVERAGE, and PivotTables for data analysis.

Article discusses calculating mean in Excel using AVERAGE function. Main issue is how to efficiently use this function for different data sets.(158 characters)

Article discusses creating, editing, and removing drop-down lists in Excel using data validation. Main issue: how to manage drop-down lists effectively.

Mastering Google Sheets Sorting: A Comprehensive Guide Sorting data in Google Sheets needn't be complex. This guide covers various techniques, from sorting entire sheets to specific ranges, by color, date, and multiple columns. Whether you're a novi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use