2016-02-12 180 views
1

我有相同的数据的两个表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值也被删除。并没有被用于所有纸张。只有活动工作表。

+0

仅供参考..他们不是公式 – Linga

回答

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

+0

非常感谢。它像一个魅力。非常感谢。很好的帮助 –