简体   繁体   中英

Excel VBA - copy and paste column then clear constants

I am copying and pasting a range to the first empty column with the code below:

Private Sub CommandButton2_Click()
Dim lastCol As Long
lastCol = Cells(4, Columns.Count).End(xlToLeft).Column

Sheets("My Sheet").Range("D4:D119").Copy
With Cells(4, lastCol + 1)
    .PasteSpecial Paste:=xlPasteAllUsingSourceTheme
    .EntireColumn.AutoFit
    .SpecialCells(xlCellTypeConstants).ClearContents
End With
End Sub

The desired outcome is to then delete the constants from the copied range, however the code above deletes all constants in the worksheet. Any help is greatly appreciated.

Thanks.

If only one cell is the source to this, the whole sheet is used. Simply change the line to:

.EntireColumn.SpecialCells(xlCellTypeConstants).ClearContents

or (if you also want to exclude row 1 to 3 in that column):

Range(Cells(4, lastCol + 1),Cells(Rows.Count, lastCol + 1)).SpecialCells(xlCellTypeConstants).ClearContents

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM