PDA

View Full Version : [SOLVED:] Sorting one Column in a Table



jarekmos
06-13-2020, 08:57 AM
Hi.
I have a problem with sorting in a table. I originally used a code that would sort column A for me before I added a table.
Here is the code:

Range("A1", Range("A1").End(xlDown)).Sort key1:=Range("A1"), order1:=xlDescending, Header:=xlYes 'sort

When I added a table, it changed it so all columns would get sorted, but I just want column A to get sorted not the rest.
Is it possible to do this in a table? To only sort one column?

Thank you and best regards.

Logit
06-13-2020, 09:18 AM
.
https://www.youtube.com/watch?v=OC3uxMXBHSc

jarekmos
06-13-2020, 10:32 AM
Thank you for the fast reply, but it's not quite what I was looking for.

At the end of the video it showed the different columns also have been filtered, but I just one column to filter itself, but the rest would stay as they are.
Is there way to do this?

Paul_Hossler
06-13-2020, 01:15 PM
When you say 'Table', do you mean a ListObject-type table, or a Range on a worksheet with rows and columns?

Maybe attaching a workbook with just the single table worksheet would b e clearer

Hightree
06-13-2020, 01:26 PM
Convert the table back to a range, sort kolom A. Convert it back to a table

jarekmos
06-13-2020, 04:29 PM
Paul,
It is a ListObject - type table.

and
Hightree,
I would like to keep the table, I don't want to convert it back to a range. I would like to know how to sort one column in the table, but leave the other columns as they are. Is there a way to do this?

p45cal
06-15-2020, 01:59 PM
It looks like you may have to do this:
1. Use the table's grab handle in the bottom right corner to reduce it to one column
2. Sort that column
3. Use the grab handle to restore the full extent of the table.

In a macro (make sure that a cell in the table is selected first):
Sub blah()
With Selection.ListObject
Set OrigSize = .Range
.Resize .ListColumns(1).Range
.Sort.SortFields.Clear
.Sort.SortFields.Add2 Key:=.ListColumns(1).Range, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
With .Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
.Resize OrigSize
End With
End Sub

Paul_Hossler
06-15-2020, 03:20 PM
I would like to know how to sort one column in the table, but leave the other columns as they are. Is there a way to do this?

No clean way it looks like

Why do you want to do a sort like this since it seems counter to the basic table paradigm?

snb
06-16-2020, 01:36 AM
Sub M_snb()
Sheet1.ListObjects(1).ListColumns(3).Range.Copy Sheet1.Cells(1, 20)
Sheet1.Cells(1, 20).CurrentRegion.Sort Sheet1.Cells(1, 20), 2, , , , , , 1
End Sub

jarekmos
06-19-2020, 01:39 PM
Thank you for all of the replies.

p45cal, your code works nicely and it's what I will be using.