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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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