what could be a better way of writing these codes.
they worked but looked messy.
many many thanks
they worked but looked messy.
many many thanks
Code:
Private Sub CommandButton1_Click()
x = Val(InputBox("(1) = A (2) = B (3) = C (4) = D (5) = E", "Select Column to sort (1~5).."))
If x = 0 Then
Exit Sub
End If
If x < 1 Or x > 5 Then
MsgBox "Enter 1 ~ 5 only..", vbCritical
Exit Sub
End If
If x = 1 Then
Call SortCol("A", "A1:E", "A1:A")
End If
If x = 2 Then
Call SortCol("B", "A1:E", "B1:B")
End If
If x = 3 Then
Call SortCol("C", "A1:E", "C1:C")
End If
If x = 4 Then
Call SortCol("D", "A1:E", "D1:D")
End If
If x = 5 Then
Call SortCol("E", "A1:E", "E1:E")
End If
Range("A2").Select
End Sub
Sub SortCol(mcol As String, mrng As String, mrng1 As String)
lastrow = Cells(Rows.Count, mcol).End(xlUp).Row
Range(mrng & lastrow).Select
ActiveWorkbook.Worksheets("Sheet1").sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").sort.SortFields.Add Key:=Range(mrng1 & lastrow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").sort
.SetRange Range(mrng & lastrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub