VBA sort

Tom F

Active Member
Joined
Oct 19, 2002
Messages
263
Column A (last name), B (first name), and C (grade)

What is the VBA code to sort these three columns based on Column C in increasing order? Columns A,B,C will end right before the last non-blank cell in their respective column.

I’m new to VBA and trying to get it figured out.

Thanks for your guidance and coaching,
Tom
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Tom

Try the macro recorder for starts. That is the best way for you to learn versus somone just giving you the code.

Record your steps as you go to Data > Sort > Sort by (col C) > Ascending or Desceding

If you have problems, post back

Harry
 
Upvote 0
Sort By MI

Try this.
Code:
Sub SortByMI()
'
' SortByMI Macro
'

    Dim count As Integer
    
    count = WorksheetFunction.CountA(ActiveSheet.Range("A:A")) - 1
    
    Range("A1:C1").Select
    Range("C1").Activate
    Range(Selection, Selection.End(xlDown)).Select
    Range("A1:C" & count).Select
    Range("C1").Activate
    Selection.Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    Range("A1").Select
End Sub

I just recorded this macro and modified it with the desired number of cells to sort.
 
Upvote 0
kmillen,

a run-time error '1004': application defined or object-defined error was displayed after executing the code.

The line

Selection.Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

was highlighted in yellow.
 
Upvote 0
try
Code:
Sub test()
With Range("a1", Range("c" & Rows.Count).End(xlUp))
     .Sort key1:=.Cells(1,3), order1:=xlAscending, header:=xlGuess
End With
 
Upvote 0
Thanks Jindon,

After adding the End Sub it worked like a charm.

I'll need some time to process this to see exactly how it works.

Thanks to all who contribute to make this a successful, helpful board,
Tom
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

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