Why is this not working - VBA to Copy from One Sheet to Many

lemo

Registered User.
Local time
Today, 07:11
Joined
Apr 30, 2008
Messages
187
Howdy, it's been a while, hope everyone is doing fine.

I have this rather simple code below that runs without errors, but it doesn't actually do what it supposed to do - copy a range of cells from one sheet to other sheets. Nothing is copied for some reason.
Could you help please.
Thanks in advance!

Code:
Sub copytoalldates()
    Dim ws As Worksheet
    Dim rng As Range
    Set rng = ActiveWorkbook.Worksheets("June 18").Range("L24:P46")
    For Each ws In ActiveWorkbook.Worksheets
        If Not (ws.Name = "Year Calculator" Or ws.Name = "June 18") Then
            With ws
                .Unprotect
                .Range("R24:V46") = rng
                .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
            End With
        End If
    Next ws
End Sub
 
create a macro that will do the task for you.
copy the macro to your sub editing as needed.
 
Another thing, have you stepped through the code to see that the cod is actually executing as you expect.
 
I stepped through, nothing shows up on the first few sheets where I expect the line .Range("R24:V46") = rng to copy my source range to the destination.
 
Ok, made it work by using

ws.Unprotect
rng.Copy Destination:=ws.Range("R24:V46")
ws.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

instead of

With ws
.Unprotect
.Range("R24:V46") = rng
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With

too bad, the second choice looks more elegant to me, but couldn't make it work.
thanks for help!
 

Users who are viewing this thread

Back
Top Bottom