Sorting Layer/Level Names
When developing VBA macros for either AutoCAD or MicroStation it is often necessary to display a list of the layers/levels in the active file in either a ListBox control or ComboBox control. Unfortunately the layer/level names are not sorted in alphabetical order and the VBA UserForm controls do not have their own Sort methods. Using a standard bubble sort routine does a nice job of sorting a string array of layer/level names.
Public Sub BubbleSort(ByRef arr() As String, Optional numEls = -1 As Long, Optional
Descending As Boolean = False) Dim Value As String Dim index As Long Dim firstItem As Long Dim indexLimit As Long Dim lastSwap As Long ' account for optional arguments If numEls = -1 Then numEls = UBound(arr) End If firstItem = LBound(arr) lastSwap = numEls Do indexLimit = lastSwap - 1 lastSwap = 0 For index = firstItem To indexLimit Value = arr(index) If (Value > arr(index + 1)) Xor Descending Then ' if the items are not in order, swap them arr(index) = arr(index + 1) arr(index + 1) = Value lastSwap = index End If Next Loop While lastSwap End Sub
Envision is a leading provider of CAD related solutions to engineers, surveyors, and GIS professionals. We have been providing expert solutions for CAD, Civil and Geospatial applications for both government agencies and AEC firms since 1996.
One Response to “Sorting Layer/Level Names”
Envision Newsletter May 2010 - Envision CAD | MicroStation Training & InRoads Training Says:
[...] VBA: Sorting Layer/Level Names [...]