VBA & Macro Problems

How to Copy Worksheets Based on Cell Values Using Excel VBA

Emma BrownEmma Brown Oct 28, 2026 869 views

Question details

The user wants to automate the process of copying existing worksheets and renaming the new copies based on a list of names specified in worksheet cells.

How to Copy Worksheets Based on Cell Values Using Excel VBA
Product
Excel
Device & OS
not provided
Scenario
Automating repetitive worksheet duplication and renaming using VBA macros based on a structured list in a spreadsheet column.
Observed behavior
The user needs a script to loop through columns, verify if the source sheet exists, copy it, and apply the new name without triggering the 'Object variable or With block variable not set' error.
Before you start

Ensure your workbook is saved as an Excel Macro-Enabled Workbook (.xlsm) and that the Developer tab is visible on your ribbon to access the VBA editor.

Solution 1Recommended

Use a VBA Macro to Loop Through Cells and Copy Worksheets

Create a VBA loop that reads source names from one column, checks for their existence, copies the sheets, and renames them using a secondary column.

By explicitly declaring Worksheet and Range objects rather than passing index values directly into an If statement, you can avoid common 'Object variable not set' errors. This script will loop through column E for source names and use column F for the new names.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

Click on 'Insert' in the top menu and select 'Module' to create a blank script window.

3
Define the Macro and Loop

Write a VBA subroutine that defines a range (e.g., Range("E2:E100")). Create a 'For Each' loop to iterate through every cell in this target range.

4
Assign and Verify Objects

Inside the loop, set a Worksheet variable to the sheet name listed in the current cell. Use error handling (On Error Resume Next) before setting the variable to check if the worksheet actually exists before copying it.

5
Copy and Rename

If the worksheet exists, use 'ws.Copy After:=Sheets(Sheets.Count)'. Then rename the newly active sheet by referencing the offset cell using 'ActiveSheet.Name = cell.Offset(0, 1).Value'.

6
Run the Macro

Close the VBA editor and run the macro via the Developer tab > Macros, or by pressing F5 within the editor.

Use a VBA Macro to Loop Through Cells and Copy Worksheets
Best Practice: Always reset your error handling with 'On Error GoTo 0' after verifying the worksheet object. This ensures other potential errors in your script are still caught.
Efficient Spreadsheet Automation

Automate Worksheets in WPS Office Spreadsheets

WPS Office offers robust support for VBA macros, allowing you to run complex automation scripts like copying and renaming worksheets based on cell values seamlessly.

  1. 1. Open your file in WPS: Launch WPS Spreadsheets and open your Macro-Enabled Workbook (.xlsm).
  2. 2. Access the Macro Editor: Navigate to the 'Developer' tab on the top ribbon and click on 'Macros', or press Alt + F11 to open the WPS Macro Editor.
  3. 3. Execute the Script: Paste your VBA code to copy and rename sheets into a new module, then click 'Run' to execute the automation instantly.
Fully compatible with Microsoft Excel VBA scripts and macros.Easily loop through columns to copy and rename multiple worksheets dynamically.Free and lightweight alternative for powerful spreadsheet data manipulation.
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get an 'Object variable or With block variable not set' error in VBA?

This error usually occurs when the macro tries to reference a worksheet or range object that hasn't been properly assigned or doesn't exist. For instance, if the cell in Column E contains a sheet name that doesn't exist in the workbook, attempting to copy it will throw this error.

How can I copy a worksheet to the very end of the workbook?

In your VBA script, use the syntax 'Worksheets("SourceSheet").Copy After:=Worksheets(Worksheets.Count)'. This ensures the duplicated worksheet is placed after the last existing tab.

How do I ignore blank cells when looping through a column in VBA?

Inside your 'For Each' loop, wrap your copy and rename logic in an 'If cell.Value <> "" Then' statement. This tells VBA to skip any empty rows in your list and only process cells with actual text.