
01-05-2006, 08:38 AM
|
|
Newcomer
|
|
Join Date: Dec 2005
Posts: 5
|
|
3 key Sort doesn't Work
|
Hi
I am getting an Application-defined or Object-defined error on the r.sort section. The sort works fine if I delete key 2 and 3. Can anyone help me with some ideas to make it work.
Thanks
Here is the code:
Code:
Private Sub ok_Click()
Dim s1 As Integer, s2 As Integer, s3 As Integer 'sort column
Dim o1 As Integer, o2 As Integer, o3 As Integer 'sort order
Dim r As Range, Ord As Long
'set the region and initial sort criteria
Set r = Range("A5:k65") '.CurrentRegion
If cboSort1.ListIndex = 1 Then
s1 = 1
GoTo Ord
End If
If cboSort1.ListIndex = 2 Then
s1 = 2
End If
If cboSort1.ListIndex = 3 Then
s1 = 3
MsgBox cboSort1.Text
End If
If cboSort1.ListIndex = 4 Then
s1 = 4
End If
If cboSort1.ListIndex = 5 Then
s1 = 5
End If
If cboSort1.ListIndex = 6 Then
s1 = 6
End If
If cboSort1.ListIndex = 7 Then
s1 = 7
End If
If cboSort1.ListIndex = 8 Then
s1 = 8
End If
If cboSort1.ListIndex = 9 Then
s1 = 9
End If
If cboSort1.ListIndex = 10 Then
s1 = 10
End If
If cboSort1.ListIndex = 11 Then
s1 = 11
End If
If cboSort2.ListIndex = 1 Then
s2 = 1
End If
If cboSort2.ListIndex = 2 Then
s2 = 2
End If
If cboSort2.ListIndex = 3 Then
s2 = 3
End If
If cboSort2.ListIndex = 4 Then
s2 = 4
End If
If cboSort2.ListIndex = 5 Then
s2 = 5
End If
If cboSort2.ListIndex = 6 Then
s2 = 6
End If
If cboSort2.ListIndex = 7 Then
s2 = 7
End If
If cboSort2.ListIndex = 8 Then
s2 = 8
End If
If cboSort2.ListIndex = 9 Then
s2 = 9
End If
If cboSort2.ListIndex = 10 Then
s2 = 10
End If
If cboSort2.ListIndex = 11 Then
s2 = 11
End If
If cboSort3.ListIndex = 1 Then
s3 = 1
End If
If cboSort3.ListIndex = 2 Then
s3 = 2
End If
If cboSort3.ListIndex = 3 Then
s3 = 3
End If
If cboSort3.ListIndex = 4 Then
s3 = 4
End If
If cboSort3.ListIndex = 5 Then
s3 = 5
End If
If cboSort3.ListIndex = 6 Then
s3 = 6
End If
If cboSort3.ListIndex = 7 Then
s3 = 7
End If
If cboSort3.ListIndex = 8 Then
s3 = 8
End If
If cboSort3.ListIndex = 9 Then
s3 = 9
End If
If cboSort3.ListIndex = 10 Then
s3 = 10
End If
If cboSort3.ListIndex = 11 Then
s1 = 11
'set the initial order as ascending or descending
Ord:
If frmSort.chkBox1.Value = True Then
o1 = 1
Else: o1 = 2
End If
If frmSort.chkBox2.Value = True Then
o2 = 1
Else: o2 = 2
End If
If frmSort.chkBox3.Value = True Then
o3 = 1
Else: o3 = 2
End If
'perform sort
Worksheets("Sheet1").Range("A1:C20").Sort _
Key1:=Worksheets("Sheet1").Range("A1"), _
Key2:=Worksheets("Sheet1").Range("B1")
End If
'protect the document
End Sub
Private Sub UserForm_Initialize()
cboSort1.Clear
With frmSort.cboSort1
.RowSource = ""
.AddItem "(None)"
.AddItem Range("A4").Text
.AddItem Range("B4").Text
.AddItem Range("C4").Text
.AddItem Range("D4").Text
.AddItem Range("E4").Text
.AddItem Range("F4").Text
.AddItem Range("G4").Text
.AddItem Range("H4").Text
.AddItem Range("I4").Text
.AddItem Range("J4").Text
.AddItem Range("K4").Text
End With
cboSort2.Clear
With frmSort.cboSort2
.RowSource = ""
.AddItem "(None)"
.AddItem Range("A4").Text
.AddItem Range("B4").Text
.AddItem Range("C4").Text
.AddItem Range("D4").Text
.AddItem Range("E4").Text
.AddItem Range("F4").Text
.AddItem Range("G4").Text
.AddItem Range("H4").Text
.AddItem Range("I4").Text
.AddItem Range("J4").Text
.AddItem Range("K4").Text
End With
cboSort3.Clear
With frmSort.cboSort3
.RowSource = ""
.AddItem "(None)"
.AddItem Range("A4").Text
.AddItem Range("B4").Text
.AddItem Range("C4").Text
.AddItem Range("D4").Text
.AddItem Range("E4").Text
.AddItem Range("F4").Text
.AddItem Range("G4").Text
.AddItem Range("H4").Text
.AddItem Range("I4").Text
.AddItem Range("J4").Text
.AddItem Range("K4").Text
End With
End Sub
Edit by italkid: The use of [vb]][ and [/vb] tags makes your code better readable...
|
Last edited by italkid; 01-05-2006 at 08:50 AM.
|