Option Explicit
Const sASC As String = "Ascending", sDESC As String = "Descending"
Private Sub CommandButton1_Click()
'reset form button
UserForm_Activate
End Sub
Private Sub CommandButton2_Click()
' Remove 2nd sort column
EnableComboBox ComboBox2, False
End Sub
Private Sub CommandButton3_Click()
' Remove 3rd sort column
EnableComboBox ComboBox3, False
End Sub
Private Sub CommandButton4_Click()
' plus button (add 2nd sort column)
EnableComboBox ComboBox2, True
End Sub
Private Sub CommandButton5_Click()
' plus button (add 3rd sort column)
EnableComboBox ComboBox3, True
End Sub
Private Sub CommandButton6_Click()
' Sort button
Dim rTbl As Range
Set rTbl = ActiveSheet.Range("C2").CurrentRegion
With rTbl
Select Case True
Case ComboBox3.Enabled
.Sort key1:=.Cells(1, ComboBox1.ListIndex + Me.Tag).EntireColumn, order1:=IIf(SpinButton1.Value = 1, xlAscending, xlDescending), _
key2:=.Cells(1, ComboBox2.ListIndex + Me.Tag).EntireColumn, order2:=IIf(SpinButton2.Value = 1, xlAscending, xlDescending), _
key3:=.Cells(1, ComboBox3.ListIndex + Me.Tag).EntireColumn, order3:=IIf(SpinButton3.Value = 1, xlAscending, xlDescending), Header:=xlYes
Case ComboBox2.Enabled
.Sort key1:=.Cells(1, ComboBox1.ListIndex + Me.Tag).EntireColumn, order1:=IIf(SpinButton1.Value = 1, xlAscending, xlDescending), _
key2:=.Cells(1, ComboBox2.ListIndex + Me.Tag).EntireColumn, order2:=IIf(SpinButton2.Value = 1, xlAscending, xlDescending), Header:=xlYes
Case Else
.Sort key1:=.Cells(1, ComboBox1.ListIndex + Me.Tag).EntireColumn, order1:=IIf(SpinButton1.Value = 1, xlAscending, xlDescending), Header:=xlYes
End Select
End With
End Sub
Private Sub CommandButton7_Click()
'Cancel buttonn
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
' this runs on closing the form using close button or red X button
ProtectSht ActiveSheet.Name
End Sub
Private Sub SpinButton1_Change()
' spin button for 1st combo sort direction
If SpinButton1.Value > 1 Then SpinButton1.Value = 1
Select Case SpinButton1.Value
Case 0
Label1.Caption = sDESC
Case 1
Label1.Caption = sASC
End Select
End Sub
Private Sub SpinButton2_Change()
' spin button for 2nd combo sort direction
If SpinButton2.Value > 1 Then SpinButton2.Value = 1
Select Case SpinButton2.Value
Case 0
Label2.Caption = sDESC
Case 1
Label2.Caption = sASC
End Select
End Sub
Private Sub SpinButton3_Change()
' spin button for 3rd combo sort direction
If SpinButton3.Value > 1 Then SpinButton3.Value = 1
Select Case SpinButton3.Value
Case 0
Label3.Caption = sDESC
Case 1
Label3.Caption = sASC
End Select
End Sub
Sub HeadingComboReset(ctrlCombo As MSForms.ComboBox)
' reset a combobox
Dim vH, vCL
Dim lR As Long, UB1 As Long, iOff As Integer
Dim rTL As Range
Dim sCL As String
With ActiveSheet.Range("C1") '<<<<<<<<<< cell address of a header cell <<<<<<
Set rTL = .CurrentRegion.Cells(1, 1)
'load header into array
vH = WorksheetFunction.Transpose(rTL.Resize(1, .CurrentRegion.Columns.Count))
End With
UB1 = UBound(vH, 1)
' calculate offset in case header does not start in column A
iOff = rTL.Column - 1
'add Column letter to header text
For lR = 1 To UB1
vH(lR, 1) = ColLetter(lR + iOff) & " " & vH(lR, 1)
Next lR
' fill combobox with headers
With ctrlCombo
.Clear
.List = vH
.ListIndex = 0
End With
'store iOff in me.tag to use in other subs
Me.Tag = iOff
End Sub
Private Sub UserForm_Activate()
UnprotectSht ActiveSheet.Name
HeadingComboReset ComboBox1
EnableComboBox ComboBox2, True
ComboBox1.ListIndex = 3 - Me.Tag - 1 '<<<< default column C
ComboBox2.ListIndex = 26 - Me.Tag - 1 '<<<< default column Z
End Sub
Private Sub UserForm_Initialize()
EnableComboBox ComboBox2, False
EnableComboBox ComboBox3, False
End Sub
Sub EnableComboBox(cbCombo As MSForms.ComboBox, bYes As Boolean)
' sets a combobox to enabled / disable including the visibility of associated buttons and text
Dim iNr As Integer
Dim ctlBin As MSForms.CommandButton, ctlSpin As MSForms.SpinButton, _
ctlPlus As MSForms.CommandButton, ctlCtl As MSForms.Control, ctlLbl As MSForms.Label
iNr = Right(cbCombo.Name, 1)
On Error Resume Next 'not every set of controls has each type
Set ctlBin = Me.Controls("CommandButton" & iNr)
Set ctlPlus = Me.Controls("CommandButton" & iNr + 3)
Set ctlSpin = Me.Controls("SpinButton" & iNr)
Set ctlLbl = Me.Controls("Label" & iNr)
HeadingComboReset cbCombo
cbCombo.Enabled = bYes
ctlBin.Visible = bYes
ctlPlus.Visible = bYes
ctlSpin.Visible = bYes
ctlSpin.Value = 1
ctlLbl.Visible = bYes
ctlLbl.Caption = sASC
On Error GoTo 0
CommandButton6.Visible = True
End Sub