Consulting

Results 1 to 3 of 3

Thread: How to sort single column (selected)?

  1. #1
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location

    How to sort single column (selected)?

    I have code like this:

    ActiveSheet.Range("A1").Select
    ' Sort and Autofit the 2nd column in Nemocnice
    Selection.CurrentRegion.Offset(, 1).Columns(1).AutoFit
    Selection.Range("B:B").Sort Order1:=xlAscending, Header:=xlN
    The data are in column B, which is selected, and I could not find out how to specify the range for column B. Is it possibly. Would it be even possible to merge this code to one line?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    With ActiveSheet
    
        .Range(.Range("B1"), .Range("B1").End(xlDown)).Sort Key1:=.Range("B1"), Order1:=xlAscending, Header:=xlNo
    End With
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location
    I fixed the problem, there was typo in xlN => xlNo and missing key1... seems not to be optional argument.
    ActiveSheet.Range("A1").Select
    ' Sort and Autofit the 2nd column in Nemocnice
    Set DataColumn = Selection.CurrentRegion.Offset(1, 1).Columns(1)
    DataColumn.AutoFit
    DataColumn.Select
    Range(DataColumn.Address).Sort Order1:=xlAscending, Key1:=Range(DataColumn.Offset(-1).Address), Header:=xlNo
    Selection.CurrentRegion.Offset(, 1).Columns(1).AutoFit
    Last edited by vangog; 12-27-2021 at 12:02 PM. Reason: fixed

Posting Permissions

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