Copy an Excel Row Without Constants Using VBA | WPS Support
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.

- 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.
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.
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.
Press ALT + F11 to open the Visual Basic for Applications (VBA) Editor, and insert a new Module.
Write a line in your macro to copy the desired source row, for example: Range("1:1").Copy.
Select your destination row and paste the formats and formulas using the PasteSpecial method.
Apply the ClearContents method targeting constants on the newly pasted row: Selection.SpecialCells(xlCellTypeConstants).ClearContents.
Wrap the SpecialCells command with 'On Error Resume Next' and 'On Error GoTo 0' to prevent runtime errors if the row contains no constants.

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. Install WPS Office: Download and install the free version of WPS Office from the official website.
- 2. Open Your Macro File: Open your Macro-Enabled Excel workbook (.xlsm) directly in WPS Spreadsheets.
- 3. Access Developer Tools: Navigate to the Developer tab on the top ribbon to access the 'Macros' or 'Visual Basic' tools.
- 4. Run or Edit Your Script: Run your row-copying macro directly within the WPS VBA editor without needing to rewrite your code.

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.




