VBA: Sorting a Table?

Kentetsu

Well-known Member
Joined
Jan 22, 2004
Messages
520
I don't know why I'm having such difficulty with this. I've been searching all day and have tried 20 variations of code, but nothing seems to work.

I've got this doing everything I want it to, all I need to add is a sorting function (Ascending, Column C, table has headers)

Here's what I've got so far:
Rich (BB code):
With Sheets("Names")
    .Visible = True
    .Unprotect Password:="*****"
    
Set the_sheet = Worksheets("Names")
Set table_list_object = the_sheet.ListObjects("Table1")
    'Add a row to the Table
Set table_object_row = table_list_object.ListRows.Add
        last_row_with_data = the_sheet.Range("Table1").End(xlUp).Row
        Sheets("Add New User").Range("E5:E6").Copy
        .Cells(Rows.Count, "B").End(xlUp)(1).PasteSpecial Paste:=xlPasteValues, _
        Transpose:=True
        
'        Here I Need to Sort Ascending on Column C
        
        .Protect Password:="*****"
        .Visible = xlSheetVeryHidden
End With

Any assistance is appreciated...
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
this is the sort function I use. I just call on it and then pass it the array. if this isn't what you need just record a macro of you sorting it in excel.

Code:
Private Function sortA(atbs As Variant)
Dim cnt As Integer, i As Integer
Dim tempA As Variant
tempA = 0
i = 0
For cnt = 0 To UBound(atbs)
     i = cnt
     While i < UBound(atbs)
          If atbs(cnt) <= atbs(i) Then
               'do nothing
          Else: tempA = atbs(cnt)
               atbs(cnt) = atbs(i)
               atbs(i) = tempA
          End If
               i = i + 1
     Wend
Next
End Function
 
Upvote 0
I came up with the following, but get an error halfway through (bolded section):

Rich (BB code):
With Sheets("Names")
        .ListObjects("Table1").Sort.SortFields.Clear
        .ListObjects("Table1").Sort.SortFields.Add _
        Key:=Range("Table1[[#All],[Last]]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
        .ListObjects("Table1").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0
Got it working with this:

Code:
With target_table.Sort
        .SortFields.Clear
        .SortFields.Add Key:=sort_column _
        , SortOn:=xlSortOnValues, Order:=xlAscending _
        , DataOption:=xlSortNormal
        .Apply
    End With
 
Upvote 0
only reason I suggested the function I posted is, you write it once and when ever you need something sorted from any other sub you just call the sort function and pass it what ever you need sorted. then you don't need to re-write sort code in every sub you have. just makes your program a bit more neat and organized
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,945
Members
449,198
Latest member
MhammadishaqKhan

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