If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Fastest Way to Sort Rows

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-21-05, 09:00
mBlack mBlack is offline
Registered User
 
Join Date: Jan 2004
Posts: 9
Fastest Way to Sort Rows

Can anyone share a fast way to sort rows (ascending or descending) in each section of a worksheet based on the value of a specified column using VBA?

I have working code but there are many lines and seems very cumbersome. I know there is a quicker way to do it that I am not aware of.

Attached is a workbook with an example of what I am trying to accomplish. I need to sort the rows in each section by Flight/Truck number or by the ETA/ETD.

Any help would greatly be appreciated!

Mike
Attached Files
File Type: zip TestWorkbook.zip (4.3 KB, 31 views)
Reply With Quote
  #2 (permalink)  
Old 10-21-05, 10:52
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
Howdy.

Here is code that will do this.

Code:
Sub test()
Application.ScreenUpdating = False
    Range("A5:F14").Sort Key1:=Range("A4"), Order1:=xlAscending
    Range("A19:F28").Sort Key1:=Range("A18"), Order1:=xlAscending
    Range("A33:F42").Sort Key1:=Range("A32"), Order1:=xlAscending
    Range("A47:F56").Sort Key1:=Range("A46"), Order1:=xlAscending
Application.ScreenUpdating = True
End Sub
However, if you will be having more items in each section, it might be better to have each one on its own worksheets. Or, putting them horizontally so that they can grow vertically. I would suggest using Rows.Count to then determine how many items to sort for each section.
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums

Last edited by shades; 10-21-05 at 10:55.
Reply With Quote
  #3 (permalink)  
Old 10-21-05, 11:19
mBlack mBlack is offline
Registered User
 
Join Date: Jan 2004
Posts: 9
Thanks for the info! I guess I should have given more information. Each section has the potential to grow or shrink based on the day and the number of flights / truck routes. I have code that dynamically re-sizes each section accordingly. This is where the sorting gets a little more complicated since the ranges are dynamic.
Reply With Quote
  #4 (permalink)  
Old 10-21-05, 12:58
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
I think all the more reason to separate the data into separate columns or even worksheets. Then the dynamic growth is easier to maintain and code. And it should be faster.
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On