1

I have two sheets of same data

A  B  C
   5  6
4  3  3 

Formula 1

Sub Button1_Click()

Dim Current As Worksheet


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

End Sub

The formula works for me. But I need to apply this script to all sheets,

Formula 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

--> It works but values in A1 also deleted. And it is not being used for all sheets. Only active sheets.

1
  • FYI.. they are not formula Commented Feb 12, 2016 at 5:25

1 Answer 1

3

A With ... End With statement can carry a parent worksheet reference along in a block of commands but you must prefix each .Range or .Cells reference with a period (aka full stop) to accept the parent worksheet relationship.

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

Note .Range and not Range.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. It works like a charm. Thank you so much. Great help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.