Sorting table macro, run time error 1004

Limone

Board Regular
Joined
Dec 20, 2018
Messages
57
[FONT=&quot]Hi everyone. I've got a button that's supposed to sort a table when clicked but it's giving me a run time 1004 error. Any idea why? The .Apply line gets highlighted.

It happens wheter the sheet is protected or not.[/FONT]

[FONT=&quot]
[/FONT]

[FONT=&quot]
[/FONT]

Code:
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">Sub PulsanteRiordinaA_Click()
Application.Screenupdating=False
Activesheet.Unprotect</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">Dim lo As Excel.ListObject</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]
[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">Set lo = ActiveWorkbook.Worksheets("Dati Anagrafici").ListObjects("TabellaAnagrafe")</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">With lo</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">.Sort.SortFields.Clear</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">.Sort.SortFields.Add _</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">Key:=Range("TabellaAnagrafe"), SortOn:=xlSortOnValues, Order:=xlAscending, _</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">DataOption:=xlSortNormal</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">With .Sort</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">.Header = xlYes</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">.MatchCase = False</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">.Orientation = xlTopToBottom</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">.SortMethod = xlPinYin</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">.Apply
Activesheet.Protect
Application.Screenupdating  = True</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">End With</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">End With</code>[/FONT][/COLOR]
[COLOR=#1C1C1C][FONT=&quot]<code class="s90z9tc-7 klSgyG" style="margin: 0px 2px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Consolas, "Courier New", monospace; vertical-align: baseline; background: rgba(238, 238, 238, 0.8); color: rgb(255, 0, 109); max-width: 100%; overflow: auto;">End Sub</code>[/FONT][/COLOR]
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,216,028
Messages
6,128,387
Members
449,445
Latest member
JJFabEngineering

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