VBA sort table based on one column

Melimob

Active Member
Joined
Oct 16, 2011
Messages
396
Office Version
  1. 365
Hi

I have the below code but realise believe it is sorting only one column which means the other columns will be out of sync?

Code:
With ActiveWorkbook.Worksheets("Tech FootPrint")
        
        lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
        .Sort.SortFields.Clear


        .Sort.SortFields.Add Key:=.Range("B5").Resize(lastrow - 3), _
                             SortOn:=xlSortOnValues, _
                             Order:=xlAscending, _
                             DataOption:=xlSortNormal
        .Sort.SetRange .Range("B5").Resize(lastrow - 3)
        .Sort.Header = xlNo
        .Sort.MatchCase = False
        .Sort.Orientation = xlTopToBottom
        .Sort.SortMethod = xlPinYin
        .Sort.Apply
    End With

Here's a recorded macro that will sort the whole table based on column B:

Code:
 Range("Table16[[#Headers],[Client]]").Select    ActiveWorkbook.Worksheets("Tech FootPrint").ListObjects("Table16").Sort. _
        SortFields.Clear
    ActiveWorkbook.Worksheets("Tech FootPrint").ListObjects("Table16").Sort. _
        SortFields.Add Key:=Range("Table16[Client]"), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Tech FootPrint").ListObjects("Table16").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

I'm happy to replace with the recorded code however unsure what the 'resize' piece is doing from the 1st piece of code and whether I can include it in the new?

Code:
Range("B5").Resize(lastrow - 3), _

I should mention that I have a a totals row!

advice appreciated!
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

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