View Full Version : sort ascending using VBA
9th Oct 2009, 10:05 am
I need some help with sorting programming in AutoCAD VBA. I have an excel sheet saved as .csv. I need to sort this .csv file in ascending order based on a particular column. The location of that reference column is also fixed.
Please find the attached .csv. In this eg., I have six columns. I need to sort the whole sheet in ascending order based on the sixth column i.e. Column F.
9th Oct 2009, 11:14 am
What would be the end result? By that I mean: If the end result is a .CVS or .XLS then sorting the original with Excel would be quite easy. If Excel (or something similar) is no longer part of the process then I’d suggest:
Set up a Type to contain individual rows of data
Public Type CVSData
A As Integer
B As Double
C As Double
D As String
E As String
F As Double
Create a new Collection.
Start iterating through the CVS file to create an instance of CVSData for each row. Dim typRow as CVSData
For the first line just add the typRow to the Collection. Collection.Add typRow
For subsequent lines, iterate through the items already in the collection to find the first place where Collection.Item.F > typRow.F.
The Collection Add method has these optional parameters
Collection.Add Item, [Key], [Before], [After]
Then Collection.Add typRow, , Collection.Item.F
Should create an ascending ordered collection.
9th Oct 2009, 12:59 pm
I ll try doing this. Actually the end result is a .csv itself. The problem is that this table is obtained as an output of one of my code. This is all being done at the back end, and finally i ve got to use this .csv to plot in as AutoCAD objects. I dont want to ask the user to go to the original file, open it and edit it.
I ll try by the procedure you've recommended. Although this kind of programming, sorting and all, is a slightly heavy for me. I might expect some help in the middle of the code if i get stuck :)!
Thanks a lot,
Powered by vBulletin™ Version 4.1.2 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.