I have a button that's supposed to alphabetically sort a table by the first column so the end user won't have problems finding employees even when the worksheet is protected.
Unfortunately the code that I have and that worked fine when I first tried it won't stop returning a run time 1004 error. The ".Apply" line gets highlighted and it happens whether the sheet is protected or not.
Any idea how to fix it?
Unfortunately the code that I have and that worked fine when I first tried it won't stop returning a run time 1004 error. The ".Apply" line gets highlighted and it happens whether the sheet is protected or not.
Any idea how to fix it?
Code:
Sub PulsanteRiordinaA_Click()
Dim lo As Excel.ListObject
Application.ScreenUpdating = False
Activesheet.Unprotect
Set lo = ActiveWorkbook.Worksheets("Dati Anagrafici").ListObjects("TabellaAnagrafe")
With lo
.Sort.SortFields.Clear
.Sort.SortFields.Add _
Key:=Range("TabellaAnagrafe"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With .Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
Application.ScreenUpdating = True
Activesheet.Protect
End Sub