How to Fix VBA Macro Copying Worksheet Columns Multiple Times
Question details
The user requires assistance fixing a VBA macro that creates duplicate files or encounters errors when copying visible worksheet columns that contain only headers without underlying data.
- Product
- Spreadsheets / Excel
- Device & OS
- not provided
- Scenario
- Exporting selected and visible columns into new files using a VBA macro.
- Observed behavior
- The macro throws errors or copies columns multiple times when it processes columns that lack data rows beneath the header.
Before modifying your VBA script, prepare a sample workbook containing a mix of populated columns and header-only empty columns to safely test the logic changes without risking your actual data.
Update VBA Logic to Check for Data Below Headers
Modify your macro to verify the last used row in each selected column, preventing the code from executing copy operations on columns that only contain a header.
When macros use commands like End(xlDown) on a column with no data below row 1, they often select all rows down to the bottom of the worksheet, resulting in excessive processing loops or duplication. By explicitly checking if the last populated row is greater than 1, you can instruct the macro to skip empty columns entirely.
Press Alt + F11 on your keyboard to open the VBA Editor for your spreadsheet.
Find the loop in your script that iterates through the selected or visible columns (e.g., 'For Each col In Selection.Columns').
Inside the loop, declare a variable to find the last row using 'Cells(Rows.Count, col.Column).End(xlUp).Row'. Add an 'If' statement so the export logic only triggers if this value is greater than 1.
Run the macro on your prepared sample workbook. Verify that only columns with data are exported and that header-only columns are successfully bypassed without throwing errors.
Seamlessly Run and Edit VBA Macros with WPS Spreadsheet
WPS Office offers robust built-in support for VBA macros, allowing you to automate repetitive tasks like exporting columns efficiently without worrying about complex compatibility issues.
- 1. Enable Developer Tools: Open WPS Spreadsheet, navigate to the Options menu, and ensure the Developer Tools ribbon is enabled.
- 2. Access the Macro Editor: Click the 'Developer' tab on the ribbon, then select 'Visual Basic' to launch the built-in code editor.
- 3. Execute Your Code: Paste your updated column-checking VBA script into a module and press F5 to execute the macro smoothly.

Frequently Asked Questions
Why does End(xlDown) cause errors on empty columns?
When you use End(xlDown) on a cell that has no data directly below it, VBA jumps to the very last row of the worksheet limit. If your macro uses this to define a copy range, it will grab the entire blank column, which takes longer to process and often leads to clipboard or memory errors.
How can I count only the visible columns using VBA?
You can use the 'Range.SpecialCells(xlCellTypeVisible).Columns.Count' property in your script to determine the number of columns currently visible on the worksheet, ignoring any hidden ones during the export process.
Can I hide empty columns using a macro before copying?
Yes. You can write a macro that loops through your selected range, checks if the 'WorksheetFunction.CountA(col)' equals 1 (meaning only the header is present), and sets 'col.Hidden = True'. You can then safely copy the remaining visible cells.




