Private Sub Sort_By_B_Click()
'First Select the Range which you want to sort
Range("A2:D13").Select
'Now clear the Sort fields before sorting. This is important otherwise sorting will not take place
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B2"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
'Select the Range on which you want to perform the Sort.
'If it includes the header row as well then put .Header=xlYes, else xlNo.
.SetRange Range("A2:D13")
.Header = xlYes
'It is better to put MatchCase as False. In case you want Sorting as Case senstavie then you can make it "True"
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Private Sub Sort_By_C_Click()
'First Select the Range which you want to sort
Range("A2:D13").Select
'Now clear the Sort fields before sorting. This is important otherwise sorting will not take place
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C2"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
'Select the Range on which you want to perform the Sort.
'If it includes the header row as well then put .Header=xlYes, else xlNo.
.SetRange Range("A2:D13")
.Header = xlYes
'It is better to put MatchCase as False. In case you want Sorting as Case senstavie then you can make it "True"
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Private Sub Sort_By_D_Click()
'First Select the Range which you want to sort
Range("A2:D13").Select
'Now clear the Sort fields before sorting. This is important otherwise sorting will not take place
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("D2"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
'Select the Range on which you want to perform the Sort.
'If it includes the header row as well then put .Header=xlYes, else xlNo.
.SetRange Range("A2:D13")
.Header = xlYes
'It is better to put MatchCase as False. In case you want Sorting as Case senstavie then you can make it "True"
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Sorting.xlsm
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
Private Sub UserForm_Initialize()
Dim i As Long
Set sws = Sheets("Sheet1")
lr = sws.Cells(Rows.Count, 1).End(xlUp).Row
lc = sws.Cells(1, Columns.Count).End(xlToLeft).Column
For i = 3 To lc
Me.lstHeaders.AddItem sws.Cells(1, i)
Next i
End Sub
ASKER
ASKER
For i = 3 To 5
Set Rng = sws.Range("A2", sws.Cells(lr, lc))
sws.Sort.SortFields.Clear
Rng.Sort key1:=ColRng.Offset(1), order1:=xlAscending, Header:=xlNo
In the above code, place a line MsgBox Rng.Address, that will give you an idea about which range is being sorted by the code.ASKER
ASKER
ASKER
Set Rng = sws.Range("B2", sws.Cells(lr, lc))
Set Rng = sws.Range("B2", sws.Cells(lr, lc))
Set Rng = sws.Range("B2:H13")
Please note that the range B2:H13 should not include the header row.
ASKER
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
TRUSTED BY
You can use one of a couple of features of Excel instead.
Table
If you convert your data to a Table you will automatically get Sort and Filter options on all columns. If the data range changes, new rows of data will be automatically included; new columns should automatically be included into the table setting depending on how they are created.
Downside is that the headers cannot be calculated fields or numbers, they have to be text.
Auto Filter
By applying an Auto Filter to the data you get the same Filter and Sort options as mentioned above. Additional rows of data should get included but columns won't be. Headers can be anything you want.
Both options will give a drop-down menu option in the header row of each column from which you can select Sorting (Ascending or Descending) and filter options.