Excel VBA复制粘贴错误

时间:2016-02-12 05:18:51

标签: excel vba excel-vba

我有两张相同的数据

A  B  C
   5  6
4  3  3 

公式1

Sub Button1_Click()

Dim Current As Worksheet


  Range("A2").Copy _
  Destination:=Range("A1")
  Range("A2").ClearContents         

End Sub

该公式适合我。但我需要将此脚本应用于所有工作表,

公式2

Dim Current As Worksheet

         ' Loop through all of the worksheets in the active workbook.
         For Each Current In ThisWorkbook.Worksheets
    With Current



           Range("A2").Copy _ Destination:=Range("A1")
  Range("A2").ClearContents
            End With
         Next Current


End Sub

- >它有效,但A1中的值也被删除了。并没有用于所有床单。只有活动表。

1 个答案:

答案 0 :(得分:3)

With ... End With statement可以在命令块中携带父工作表参考,但您必须在每个.Range.Cells引用前加上句点(也称为句号)接受父工作表关系。

Dim Current As Worksheet

' Loop through all of the worksheets in the active workbook.
For Each Current In ThisWorkbook.Worksheets
    With Current
       .Range("A2").Copy Destination:=.Range("A1")
       .Range("A2").ClearContents
    End With
Next Current

注意.Range而非Range