VBA & Macro Problems

Copy an Excel Row Without Constants Using VBA | WPS Support

Emma BrownEmma Brown Nov 3, 2026 868 views

Question details

The user needs a VBA macro to copy an existing row's formulas, formatting, and data validation, but clear out any hardcoded constant values to prepare the new row for fresh input.

How to Copy an Excel Row Without Constant Values Using VBA
Product
Microsoft Excel / WPS Spreadsheets
Device & OS
not provided
Scenario
Automating data entry or creating a new record row in a template where previous structural formatting and formulas must be retained without carrying over old data.
Observed behavior
Standard copy-pasting duplicates all values, including constants. Using xlPasteFormulas pastes formulas but also carries over constant values, requiring an additional step to clear them out.
Before you start

Make sure you have enabled the Developer tab in your spreadsheet application and remember to save your workbook as a Macro-Enabled Workbook (.xlsm) to prevent losing your VBA code.

Solution 1Recommended

Copy Row and Clear Constants Using SpecialCells

The most efficient way is to copy the entire row, paste all formats and formulas, and then use the SpecialCells method on the newly pasted row to remove constants.

Spreadsheet applications treat constant values as formula paste content when using xlPasteFormulas. Because of this, copying a row without carrying over constants requires a two-step approach.

By specifically targeting xlCellTypeConstants, you safely preserve all conditional formatting, data validations, and active formulas while emptying the cells of hardcoded data.

1
Open the VBA Editor

Press ALT + F11 to open the Visual Basic for Applications (VBA) Editor, and insert a new Module.

2
Copy the Source Row

Write a line in your macro to copy the desired source row, for example: Range("1:1").Copy.

3
Paste to the Destination

Select your destination row and paste the formats and formulas using the PasteSpecial method.

4
Clear the Constants

Apply the ClearContents method targeting constants on the newly pasted row: Selection.SpecialCells(xlCellTypeConstants).ClearContents.

5
Add Error Handling

Wrap the SpecialCells command with 'On Error Resume Next' and 'On Error GoTo 0' to prevent runtime errors if the row contains no constants.

Copy Row and Clear Constants Using SpecialCells
Important Error Handling: If a row contains only formulas and blank cells, the SpecialCells method will fail and throw an error. Always use 'On Error Resume Next' before calling it.
WPS Spreadsheet Macros

Automate Your Workflows with WPS Spreadsheets

WPS Spreadsheets provides robust support for VBA macros, allowing you to run your existing automated scripts smoothly. You can automate repetitive tasks like copying rows and clearing data just as easily.

  1. 1. Install WPS Office: Download and install the free version of WPS Office from the official website.
  2. 2. Open Your Macro File: Open your Macro-Enabled Excel workbook (.xlsm) directly in WPS Spreadsheets.
  3. 3. Access Developer Tools: Navigate to the Developer tab on the top ribbon to access the 'Macros' or 'Visual Basic' tools.
  4. 4. Run or Edit Your Script: Run your row-copying macro directly within the WPS VBA editor without needing to rewrite your code.
Fully compatible with Microsoft Excel .xlsm and .xlsb macro files.Built-in Developer tools for editing and running VBA scripts seamlessly.Retains conditional formatting, data validations, and formulas precisely.Free, lightweight, and highly responsive spreadsheet environment.
microsoft office alternative - wps office

Frequently Asked Questions

Why does xlPasteFormulas also paste regular text and numbers?

In VBA, xlPasteFormulas pastes everything that would normally appear in the formula bar. For cells that do not contain a formula, the constant value itself is treated as the formula and is pasted.

Will ClearContents remove my Data Validation dropdown lists?

No, the ClearContents method only removes the cell's current inputted value. The data validation rules, dropdowns, and cell formatting remain completely intact.

How can I handle the 'No cells were found' error when clearing constants?

If a row has no constant values to clear, SpecialCells(xlCellTypeConstants) triggers an error. You can avoid this by placing 'On Error Resume Next' immediately before the code line, and 'On Error GoTo 0' right after it.

Can I specify which types of constants to clear?

Yes. SpecialCells allows a second argument to target specific data types. For example, using SpecialCells(xlCellTypeConstants, xlTextValues) will only clear text strings while leaving hardcoded numbers untouched.