Anyway to shorten "Sort A -> Z" VBA code?

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,345
Office Version
  1. 365
Platform
  1. Windows
Is there any way to simplify this code?

Steps:
1. put in filter on table (specified by range)
2. sort alphabetically on column A
3. take off filter


Code:

<font face=Calibri><SPAN style="color:#007F00">'Sort Table 1 Alphabetically</SPAN><br>LastRow_T1 = Sheet5.Cells(Rows.Count, "A").End(xlUp).Row<br><br>Sheet5.Range("A4:L" & LastRow_T1).AutoFilter<br>    Sheet5.AutoFilter.Sort.SortFields.Clear<br>    Sheet5.AutoFilter.Sort.SortFields.Add Key _<br>        :=Sheet5.Range("A4:A" & LastRow_T1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _<br>        :=xlSortTextAsNumbers<br>    <SPAN style="color:#00007F">With</SPAN> Sheet5.AutoFilter.Sort<br>        .Header = xlYes<br>        .MatchCase = <SPAN style="color:#00007F">False</SPAN><br>        .Orientation = xlTopToBottom<br>        .SortMethod = xlPinYin<br>        .Apply<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Sheet5.Range("A4:L4").AutoFilter</FONT>
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Maybe ...

Code:
    Sheet5.Range("A4").CurrentRegion.Sort Key1:=Range("A4"), _
                                          DataOption1:=xlSortTextAsNumbers, _
                                          Header:=xlYes
 
Upvote 0
I need to have the sort done in a specified range because there is another table below it that I don't want included in the sort. Will your suggestion work with that in mind?
 
Upvote 0
CurrentRegion will only work if the range you want to sort is bounded on all sides by blank rows or columns or an edge of the sheet -- which is kind of good practice anyway.
 
Upvote 0
Thanks that worked out perfectly! I did have to add in a "sheet5" to the other range though, because I am running the macro on a different sheet.

Code:

<font face=Calibri>Sheet5.Range("A4").CurrentRegion.Sort Key1:=Sheet5.Range("A4"), _<br>                                          DataOption1:=xlSortTextAsNumbers, _<br>                                          Header:=xlYes</FONT>
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,539
Members
449,038
Latest member
Guest1337

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