array sort
array sort
array sort
array sort
array sort
array sort array sort array sort array sort array sort array sort array sort array sort
array sort array sort
array sort
Go Back  Xtreme Visual Basic Talk > > > > array sort


Reply
 
Thread Tools Display Modes
  #1  
Old 07-13-2018, 08:44 PM
Heyjoe Heyjoe is offline
Centurion
 
Join Date: Nov 2006
Location: USA
Posts: 176
Default array sort


Here is the range on my workbook from a1 to b3. I want to sort this range using vba and then write the sorted range to rows 11 to 13. My sort code only sorts the first column, but does not sort the second column. How can I revise my code so the second column is included in the sort.
A B
1 105 3
2 4 2
3 88 1




The result I get is"


11 4 3
12 88 2
13 105 1


The result I want is:

11 4 2
12 88 1
13 105 4




Sub mysort()
' Sorts an array using bubble sort algorithm
Dim First As Integer, Last As Long
Dim r As Long, o As Long
Dim Temp As Long
Dim myarray As Variant



myarray = Range("a1:b3")

First = LBound(myarray)
Last = UBound(myarray)
For r = First To Last - 1
For o = r + 1 To Last
If myarray(r, 1) > myarray(o, 1) Then
Temp = myarray(o, 1)
myarray(o, 1) = myarray(r, 1)
myarray(r, 1) = Temp
End If
Next o
Next r


Range("a11:b13") = myarray



End Sub
Reply With Quote
  #2  
Old 07-14-2018, 10:12 PM
Kluz's Avatar
Kluzarray sort Kluz is offline
Sapience.Aquire

Super Moderator
* Expert *
 
Join Date: Oct 2003
Location: Lake Bluff, Ill., U.S.
Posts: 3,467
Default

Code:
Sub mysort() ' Sorts an array using bubble sort algorithm Dim First As Integer, Last As Long Dim r As Long, o As Long Dim Temp As Long, Temp2 As Long Dim myarray As Variant myarray = Range("a1:b3") First = LBound(myarray) Last = UBound(myarray) For r = First To Last - 1 For o = r + 1 To Last If myarray(r, 1) > myarray(o, 1) Then Temp = myarray(o, 1) Temp2 = myarray(o, 2) myarray(o, 1) = myarray(r, 1) myarray(o, 2) = myarray(r, 2) myarray(r, 1) = Temp myarray(r, 2) = Temp2 End If Next o Next r Range("a11:b13") = myarray End Sub
__________________
No the other right mouse click
Reply With Quote
  #3  
Old Yesterday, 04:55 AM
Heyjoe Heyjoe is offline
Centurion
 
Join Date: Nov 2006
Location: USA
Posts: 176
Default array sort

Thanks Kluz. I think that will work just fine.
Reply With Quote
Reply

Tags
sort, dim, column, myarray, temp, range, myarrayo, myarrayr, array, result, sorts, code, algorithm, rangea11b13, bubble, integer, lboundmyarray, variant, rangea1b3, uboundmyarray, vba, write, sorted, workbook, rows


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
array sort
array sort
array sort array sort
array sort
array sort
array sort array sort array sort array sort array sort array sort array sort
array sort
array sort
 
array sort
array sort
 
-->