Sorting protected table by macro, run time 1004 error

Limone

Board Regular
Joined
Dec 20, 2018
Messages
57
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?

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
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You must unprotect the sheet to work, because maybe it's not the active sheet.

Code:
Sub PulsanteRiordinaA_Click()
    Dim lo As Excel.ListObject
    Application.ScreenUpdating = False


    [COLOR=#0000ff]ActiveWorkbook.Worksheets("Dati Anagrafici").Unprotect[/COLOR]


    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


    [COLOR=#0000ff]ActiveWorkbook.Worksheets("Dati Anagrafici").Protect[/COLOR]


End Sub
 
Upvote 0
You have to put the column number to Sort, look at the updated code:

Code:
Sub PulsanteRiordinaA_Click()
    Application.ScreenUpdating = False
    ActiveWorkbook.Worksheets("Dati Anagrafici").Unprotect
    With ActiveWorkbook.Worksheets("Dati Anagrafici").ListObjects("TabellaAnagrafe").Sort
        .SortFields.Clear
        .SortFields.Add Key:=[COLOR=#0000ff]Range("TabellaAnagrafe").Columns(1)[/COLOR], _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveWorkbook.Worksheets("Dati Anagrafici").Protect
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,579
Messages
6,125,646
Members
449,245
Latest member
PatrickL

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