简体   繁体   English

Excel VBA-复制并粘贴列,然后清除常量

[英]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): 或(如果您还想排除该列的第1至3行):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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