Consulting

Results 1 to 10 of 10

Thread: Sorting one Column in a Table

  1. #1
    VBAX Regular
    Joined
    Apr 2018
    Posts
    19
    Location

    Sorting one Column in a Table

    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.

  2. #2

  3. #3
    VBAX Regular
    Joined
    Apr 2018
    Posts
    19
    Location
    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?

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Regular
    Joined
    Jan 2018
    Location
    The Netherlands
    Posts
    45
    Location
    Convert the table back to a range, sort kolom A. Convert it back to a table

  6. #6
    VBAX Regular
    Joined
    Apr 2018
    Posts
    19
    Location
    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?

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    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
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    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?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    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

  10. #10
    VBAX Regular
    Joined
    Apr 2018
    Posts
    19
    Location
    Thank you for all of the replies.

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •