Trying to Sort Column A with some Visual Basic Code

Status
Not open for further replies.

RudeBoy

Active Member
Joined
Feb 2, 2003
Messages
431
Office Version
  1. 365
Platform
  1. Windows
I have some Visual basic code that is sorting but not what I am wanting :(
I am not strong in this area so am asking for some Help - Thanks

Here is my code: I will also upload a pic of the data thanks so much for any & all help

VBA Code:
Sub Worksheet_Activate()
'
' RnkSrt Macro
' Macro add on 06/25/2024 by Rude Boy Chris
'
  rowcount = ActiveSheet.UsedRange.Rows.Count
    Range(Cells(9, 1), Cells(rowcount, 16)).Select
   
    Selection.Sort Key1:=Range("A9"), Order1:=xlAscending, _
    Header:=xlNo, OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom
    Range("A1").Activate
End Sub
 

Attachments

  • Pic3 Sorting.jpg
    Pic3 Sorting.jpg
    84.4 KB · Views: 15
Last edited by a moderator:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I guess your "Selection" doesn't start with row 9 that's why Key1 (cell A9) doesn't fit.
 
Upvote 0
I guess your "Selection" doesn't start with row 9 that's why Key1 (cell A9) doesn't fit.
When I change it to 1,1 I receive this error? Helpppppppppppp
 

Attachments

  • Error when pointing to 1-1.jpg
    Error when pointing to 1-1.jpg
    87.4 KB · Views: 10
Upvote 0
Your header starts with 9-th row (Range("A9")), but your selection starts with 1-st row (Cells(1, 1)).
 
Upvote 0
Your header starts with 9-th row (Range("A9")), but your selection starts with 1-st row (Cells(1, 1)).
I guess me & Visual Basic do not get along. I changed what you said but still an error :-( Thanks for your help

I will upload pic of error & the code I modified & ran but received an error
 

Attachments

  • Error sorting VB.jpg
    Error sorting VB.jpg
    84.5 KB · Views: 7
Upvote 0
Start with this:
VBA Code:
Sub Worksheet_Activate()
    'RnkSrt Macro
    Dim SortRange As Range, RowCount As Long, StartRow As Long
    
    With Me
        StartRow = 9
        RowCount = .UsedRange.Rows.Count
        If RowCount > StartRow Then
            Set SortRange = .Range(.Cells(9, 1), .Cells(RowCount, 16))
            SortRange.Sort Key1:=.Cells(9, 1), Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        Else
            MsgBox "No data after Row #" & StartRow & ". There is nothing to sort."
        End If
        .Range("A1").Activate
    End With
End Sub

If it is not doing what you want, then explain in detail.
 
Upvote 0
Solution
Could you upload your worksheet to cloud?
 
Upvote 0
Start with this:
VBA Code:
Sub Worksheet_Activate()
    'RnkSrt Macro
    Dim SortRange As Range, RowCount As Long, StartRow As Long
   
    With Me
        StartRow = 9
        RowCount = .UsedRange.Rows.Count
        If RowCount > StartRow Then
            Set SortRange = .Range(.Cells(9, 1), .Cells(RowCount, 16))
            SortRange.Sort Key1:=.Cells(9, 1), Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        Else
            MsgBox "No data after Row #" & StartRow & ". There is nothing to sort."
        End If
        .Range("A1").Activate
    End With
End Sub

If it is not doing what you want, then explain in detail.
That works Awesomeeeeeeeeeeeeeeeeeeeeeee
 
Upvote 0
Start with this:
VBA Code:
Sub Worksheet_Activate()
    'RnkSrt Macro
    Dim SortRange As Range, RowCount As Long, StartRow As Long
   
    With Me
        StartRow = 9
        RowCount = .UsedRange.Rows.Count
        If RowCount > StartRow Then
            Set SortRange = .Range(.Cells(9, 1), .Cells(RowCount, 16))
            SortRange.Sort Key1:=.Cells(9, 1), Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        Else
            MsgBox "No data after Row #" & StartRow & ". There is nothing to sort."
        End If
        .Range("A1").Activate
    End With
End Sub

If it is not doing what you want, then explain in detail.
I am trying to use this code to Auto Sort two Rows having the Higher number in this case 2866 be at the top.

You had given this code to me a while back & it is working great on another excel program but I am trying to use it here also.

Here is the data trying to sort

SportingNews.com Fantasy Source - NFL
10014c Joel Cheng
11003d Edgar Jurado
13019d Mark Near
286601c Robert Contreras
29001a Raymann Patao
32018b Joseph Martinez
46009d Brian Caandoy
6003c Jay Butler
80028a Willie Parawan
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!
#VALUE!#VALUE!

I am using the code you built goo stuff:

Sub Worksheet_Activate()
'RnkSrt Macro
Dim SortRange As Range, RowCount As Long, StartRow As Long

With Me
StartRow = 1
RowCount = .UsedRange.Rows.Count
If RowCount > StartRow Then
'Set SortRange = .Range(.Cells(9, 1), .Cells(RowCount, 16))
Set SortRange = .Range(.Cells(1, 1), .Cells(RowCount, 2))

'SortRange.Sort Key1:=.Cells(9, 1), Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
SortRange.Sort Key1:=.Cells(1, 1), Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Else
MsgBox "No data after Row #" & StartRow & ". There is nothing to sort."
End If
.Range("A1").Activate
End With
End Sub



The code may need a bit of tweaking let me know thanks :-)
 

Attachments

  • Excel Code-001.jpg
    Excel Code-001.jpg
    90 KB · Views: 4
Upvote 0
I am trying to use this code to Auto Sort two Rows having the Higher number in this case 2866 be at the top.

You had given this code to me a while back & it is working great on another excel program but I am trying to use it here also.

Here is the data trying to sort

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

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread.

Also, you have been asked three times in that other thread to use code tags when posting code yet you are still posting code here without the tags. Forum members are trying to help you (for no reward) with your problems. It would seem sensible to provide your information in the best possible way to make it easier for them to do so.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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