PDA

View Full Version : How to sort single column (selected)?



vangog
12-27-2021, 07:45 AM
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?

Bob Phillips
12-27-2021, 08:14 AM
With ActiveSheet

.Range(.Range("B1"), .Range("B1").End(xlDown)).Sort Key1:=.Range("B1"), Order1:=xlAscending, Header:=xlNo
End With

vangog
12-27-2021, 09:00 AM
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