Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > 3 key Sort doesn't Work


Reply
 
Thread Tools Display Modes
  #1  
Old 01-05-2006, 08:38 AM
yankari yankari is offline
Newcomer
 
Join Date: Dec 2005
Posts: 5
Default 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.
Reply With Quote
  #2  
Old 01-05-2006, 09:13 AM
italkid's Avatar
italkid italkid is offline
Down...

Retired Moderator
* Expert *
 
Join Date: Dec 2002
Location: Belgium.
Posts: 6,731
Default

You should learn to write a bit more efficient code in the first place.
Why all those If blocks?
Code:
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 End If 'etc.....longwinded stuff 'Could be replaced... s1 = cboSort1.ListIndex If s1 = 1 Then GoTo Ord s2 = cboSort2.ListIndex s3 = cboSort3.ListIndex
Your sort code should work - how does it look when you use a third key?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->