0

I have a file we share among a lot of people at work. The past five years the code has worked fine, but now a new guy is introduced to the file and he get the (links to other threads about the error) automation error on this part below:

        Dim TillfLevdagar(1 To 14) As String
        .... 
        ' code that sets some of the array items above
        .....


        Dim arr2 As Object

        Set arr2 = CreateObject("System.Collections.ArrayList") ' here it errors
        
        For Each itm In TillfLevdagar
            If itm <> "" Then arr2.Add CInt(itm)
        Next
        arr2.Sort

        For Each itm In arr2
            msg = msg & itm & ","
        Next

Since this is a work computer we are very limited in allowing any software to be installed. That includes Microsoft software.
Becuase of this I ask what alternatives do I have to the code above?

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • You could use a `Dictionary` and implement the sort by yourself, see https://stackoverflow.com/questions/14808104/sorting-a-dictionary-by-key-in-vba – FunThomas Feb 15 '22 at 14:10
  • 1
    Since `TillfLevdagar` exists and you need the `arrayList` only for sorting, create a simple function to directly sort the existing array. Then, iterate between the `TillfLevdagar` (sorted) array elements. – FaneDuru Feb 15 '22 at 14:12
  • @Andreas, does your IT dept really prevent you installing an older MS .Net framework on a new PC, even though other PCs already have it? – chris neilsen Feb 15 '22 at 19:38
  • @chrisneilsen I believe they do. If you can say no, then a no it is. – Andreas Feb 16 '22 at 13:21

1 Answers1

0

Please, try the next sorting way and use only the initial (TillfLevdagar) array:

Sub BubbleSort(arr)
    Dim i As Long, j As Long, temp
    For i = LBound(arr) To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If arr(i) > arr(j) Then
                temp = arr(i): arr(i) = arr(j)
                arr(j) = temp
            End If
        Next j
    Next i
End Sub

You can test it using the next testing procedure:

Sub testBubbleSort()
   Dim arr, arrStr(1 To 14) As String, i As Long
   arr = Split("test,boolean,core,leaf,dream,crises,bet,arrow", ",")
   For i = 1 To 14
        arrStr(i) = Chr(Int((25 + 1) * Rnd + 1) + 64) & "test"
   Next i
   BubbleSort arr
   Debug.Print Join(arr, "|")
   BubbleSort arrStr
   Debug.Print Join(arrStr, "|")
End Sub

I have this sorting sub in my testing collection Subs, from some years. I do not remember where from I found it and what I adapted, if I did that. It is a simple sorting procedure. Anyhow, I used it many times with good results.

FaneDuru
  • 38,298
  • 4
  • 19
  • 27