Command button will not work, despite include a line to unprotect and protect worksheet.

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. 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



Command Button.PNG
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Is the button on the Air outbound sheet?
If not you are un/protecting the wrong sheet
 
Upvote 0
Assuming the button is on another sheet, try
VBA Code:
Private Sub CommandButton1_Click()
Dim xlSort As XlSortOrder
Dim LastRow As Long
With Sheets("Air Outbound")
.Unprotect Password:="mozzer"

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
ActiveWorkbook.RefreshAll
.Protect Password:="mozzer", AllowFormattingColumns:=True, AllowInsertingHyperlinks:=True, AllowDeletingRows:=True

End With
End Sub
Also your If statement is redundant, so you can get rid of it.
 
Upvote 0
Solution
Thank you for the quick respondse. I'm afraid it did not change the end result. It was worth a try for sure.
 
Upvote 0
Assuming the button is on another sheet, try
VBA Code:
Private Sub CommandButton1_Click()
Dim xlSort As XlSortOrder
Dim LastRow As Long
With Sheets("Air Outbound")
.Unprotect Password:="mozzer"

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
ActiveWorkbook.RefreshAll
.Protect Password:="mozzer", AllowFormattingColumns:=True, AllowInsertingHyperlinks:=True, AllowDeletingRows:=True

End With
End Sub
Also your If statement is redundant, so you can get rid of it.
That works perfect, thank you so muich!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,019
Members
448,938
Latest member
Aaliya13

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top