I have a macro that copies rows from one sheet to another. However, I want to protect this sheet from editing. When I do this though, I cant get the macro to run. I've played around with the different locked properties and it still doesn't work.
This is what Ive attempted:
Option Explicit
Dim pwd As String
Dim ws As Worksheet
pwd = "password"
ws.Unprotect password:=pwd
Next ws
Sub FilterAndCopy()
Dim rng As Range, sht1 As Worksheet, sht2 As Worksheet
Set sht1 = Worksheets("SHIFT LOG")
Set sht2 = Worksheets("CHANGE OF NO'S")
sht2.UsedRange.ClearContents
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter Field:=1, Criteria1:="Change of Numbers"
.Range("A:F, AD:AD, BL:BO").Copy Destination:=sht2.Cells(4, "B")
.Parent.AutoFilterMode = False
.Range("F:AD").EntireColumn.Hidden = True
.Range("AE:BK").EntireColumn.Hidden = True
End With
'Go to last cell in column B
With ActiveSheet
Range("B5").Select
Selection.End(xlDown).Select
End With
End Sub
ws.Protect password:=pwd
Next ws
try this snippet code before and after your code
BEFORE: ' sheet1 and sheet2 are protected
Dim pwd As String
Dim ws as WorkSheet
pwd = "password"
For Each ws In Worksheets
ws.Unprotect Password:=pwd
Next ws
'your code
AFTER:
For Each ws In Worksheets
ws.protect Password:=pwd
Next ws
Hope this helps
EDIT post after you comment
Option Explicit
Sub FilterAndCopy()
Dim pwd As String
Dim sht1, sht2 As Worksheet
Dim rng As Range
pwd = "password"
Set sht1 = Worksheets("SHIFT LOG")
Set sht2 = Worksheets("CHANGE OF NO'S")
sht2.Unprotect Password:=pwd 'unprotect the sheet
sht2.UsedRange.ClearContents ' clear contents
With Intersect(sht1.Columns("B:BP"), sht1.UsedRange)
.Cells.EntireColumn.Hidden = False
If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
.AutoFilter Field:=1, Criteria1:="Change of Numbers"
.Range("A:F, AD:AD, BL:BO").Copy Destination:=sht2.Cells(4, "B")
.Parent.AutoFilterMode = False
.Range("F:AD").EntireColumn.Hidden = True
.Range("AE:BK").EntireColumn.Hidden = True
End With
'Go to last cell in column B
With ActiveSheet
Range("B5").Select
Selection.End(xlDown).Select
End With
sht2.Protect Password:=pwd ' protect the sheet
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With