Livin404
Well-known Member
- Joined
- Jan 7, 2019
- Messages
- 743
- Office Version
- 365
- 2019
- Platform
- Windows
Hello, I have a VBA that I think should work after the sheets are protect. When I select F5 or the Run/Sub arrow key the macro works as designed. It's only when I use the command botton, I get the error. I provided an image of the error and my macro. Thank you very much indeed.
VBA Code:
Private Sub CommandButton1_Click()
ActiveSheet.Unprotect Password:="mozzer"
Dim xlSort As XlSortOrder
Dim LastRow As Long
With Sheets("Air Outbound")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
If (.Range("A1").Value > .Range("A" & CStr(LastRow))) Then
xlSort = xlAscending
End If
.Range("A1") = "Date"
.Columns("A:I").Sort key1:=.Range("A2"), order1:=xlAscending, Header:=xlYes
End With
ActiveWorkbook.RefreshAll
ActiveSheet.Protect Password:="mozzer", AllowFormattingColumns:=True, AllowInsertingHyperlinks:=True, AllowDeletingRows:=True
End Sub