Trying to do an Auto Sort when I enter Tab using VBA Code

RudeBoy

Active Member
Joined
Feb 2, 2003
Messages
424
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,
I am trying to sort column A in my Tab & have the larger number sort to the top once I enter my tab.

It is running when I enter but my syntax must be incorrect as the largest number in Column A in Excel 365 is not sorting to the top.

Helppppppp Please advise - Thanks so much

Here is the VBA code I am using:
VBA Code:
Sub Worksheet_Activate()
'
' RnkSrt Macro
'
    RowCount = ActiveSheet.UsedRange.Rows.Count
    Range(Cells(1, 1), Cells(RowCount, 2)).Select
   
    Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, _
    Header:=xlNo, OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom
    Range("A1").Activate
End Sub
 
Last edited by a moderator:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Strange ... your macro code works here as is. I'm using Excel 2007 on Win 10/64 bit
 
Upvote 0
the largest number in Column A in Excel 365 is not sorting to the top.
That is because your code has Order1:=xlAscending when it should be Order1:=xlDescending
if you want largest at the top
Also ..
  1. When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time.

  2. I suggest that you update your forum profile (click your user name at the top right of the forum, then ‘Account details’) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
That is because your code has Order1:=xlAscending when it should be Order1:=xlDescending
if you want largest at the top
Also ..
  1. When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time.

  2. I suggest that you update your forum profile (click your user name at the top right of the forum, then ‘Account details’) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
Thanks 🙏 i will try that when i return home
 
Upvote 0
Thanks 🙏 i will try that when i return home
Put Descending but I still see the numbers below values are still not sorting properly? Please advise thanks


SportingNews.com Fantasy Source - NFL
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
80028a Willie Parawan
6003c Jay Butler
46009d Brian Caandoy
32018b Joseph Martinez
29001a Raymann Patao
286601c Robert Contreras
13019d Mark Near
11003d Edgar Jurado
10014c Joel Cheng
 
Upvote 0
Thanks 🙏 i will try that when i return home
Here is the VBA code is a setting not on or something? Helpppppp

Sub Worksheet_Activate()
'
' RnkSrt Macro
' Modified on 07/26/2004 by Rude Boy Chris
'
RowCount = ActiveSheet.UsedRange.Rows.Count
Range(Cells(1, 1), Cells(RowCount, 2)).Select

Selection.Sort Key1:=Range("A1"), Order1:=xlDescending, _
Header:=xlNo, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Range("A1").Activate
End Sub
 
Upvote 0
Strange ... your macro code works here as is. I'm using Excel 2007 on Win 10/64 bit
Yep this VBA sort has worked for years on other version of Excel but does not seem happy on 365 version?? Maybe a different setting is not on not sure?
 
Upvote 0
Do those #VALUE! cells contain formulas or have they been converted to text values only?
In any case the sort will treat them as text values and text values are always treated by Excel as greater than numerical values so will get sorted to the top with a descending sort followed by numbers in descending order. This is exactly what happens with your data and code once Ascending is changed to Descending, so I don't see anything happening incorrectly

Before:
RudeBoy.xlsm
AB
1#VALUE!#VALUE!
2#VALUE!#VALUE!
3#VALUE!#VALUE!
4#VALUE!#VALUE!
5#VALUE!#VALUE!
6#VALUE!#VALUE!
7#VALUE!#VALUE!
8#VALUE!#VALUE!
9#VALUE!#VALUE!
10#VALUE!#VALUE!
11#VALUE!#VALUE!
1280028a Willie Parawan
136003c Jay Butler
1446009d Brian Caandoy
1532018b Joseph Martinez
1629001a Raymann Patao
17286601c Robert Contreras
1813019d Mark Near
1911003d Edgar Jurado
2010014c Joel Cheng
Sheet1


Rich (BB code):
Sub Worksheet_Activate()
'
' RnkSrt Macro
'
    RowCount = ActiveSheet.UsedRange.Rows.Count
    Range(Cells(1, 1), Cells(RowCount, 2)).Select
   
    Selection.Sort Key1:=Range("A1"), Order1:=xlDescending, _
    Header:=xlNo, OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom
    Range("A1").Activate
End Sub

After:
RudeBoy.xlsm
AB
1#VALUE!#VALUE!
2#VALUE!#VALUE!
3#VALUE!#VALUE!
4#VALUE!#VALUE!
5#VALUE!#VALUE!
6#VALUE!#VALUE!
7#VALUE!#VALUE!
8#VALUE!#VALUE!
9#VALUE!#VALUE!
10#VALUE!#VALUE!
11#VALUE!#VALUE!
12286601c Robert Contreras
1380028a Willie Parawan
1446009d Brian Caandoy
1532018b Joseph Martinez
1629001a Raymann Patao
1713019d Mark Near
1811003d Edgar Jurado
1910014c Joel Cheng
206003c Jay Butler
Sheet1
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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