Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-复制并粘贴列,然后清除常量_Excel_Vba - Fatal编程技术网

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

Excel VBA-复制并粘贴列,然后清除常量,excel,vba,Excel,Vba,我正在将一个范围复制并粘贴到第一个空列,代码如下: 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:=xlPasteAllUsingSourc

我正在将一个范围复制并粘贴到第一个空列,代码如下:

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
理想的结果是从复制的范围中删除常量,但是上面的代码会删除工作表中的所有常量。非常感谢您的帮助


谢谢。

如果只有一个单元格是源,则使用整个工作表。只需将行更改为:

.EntireColumn.SpecialCells(xlCellTypeConstants).ClearContents
或者(如果您还想排除该列中的第1行到第3行):


如果只有一个单元格是源,则使用整个工作表。只需将行更改为:

.EntireColumn.SpecialCells(xlCellTypeConstants).ClearContents
或者(如果您还想排除该列中的第1行到第3行):


感谢您的帮助,Range(单元格(4,lastCol+1),Cells(Rows.Count,lastCol+1)).SpecialCells(xlCellTypeConstants)。ClearContents成功了。还有一个问题-代码只复制范围并粘贴到第一个空列一次,当我再次单击命令按钮时,它似乎只是复制并粘贴到同一列中,不是预期的下一个空列。有什么建议吗?谢谢。您是否再次运行了
lastCol=…
行?第4行的单元格被使用了吗?谢谢你的帮助,范围(单元格(4,lastCol+1),单元格(Rows.Count,lastCol+1)).SpecialCells(xlCellTypeConstants)。ClearContents解决了这个问题。还有一个问题-当我再次单击命令按钮时,代码只复制范围并将其粘贴到第一个空列一次,它似乎只是复制并粘贴到同一列中,而不是预期的下一个空列。有什么建议吗?谢谢。您是否再次运行了
lastCol=…
行?第4行的单元格是否已使用?