-1

I have an arrayList created as:

Dim coll As Object
Set coll = CreateObject("System.Collections.ArrayList")
coll.Add "240,70,90,45,62,255,255"
coll.Add "255,70,90,23,54,56,123"
...
...

and a function that accepts an array to work with individual elements such as (240,70,90,45,62,255,255), not "255,70,90,23,54,56,123". How can I convert my "string of elements" into individual elements? Keep in mind I need the array to be of type Variant. I can't use a string. I had to create an arrayList initially because I could not create a massive array such as

buf = Array(198, 132, 36, 170, 0, 0, 0, 102, 198, 132, 36..............)

I need to pass an array of type Variant in order to be able to use it in this function below:

Sub WriteBinary(FileName, buf)

    Dim I, aBuf, Size, bStream
    Size = UBound(buf): ReDim aBuf(Size \ 2)
    For I = 0 To Size - 1 Step 2
        aBuf(I \ 2) = ChrW(buf(I + 1) * 256 + buf(I))
    Next

    If I = Size Then aBuf(I \ 2) = ChrW(buf(I))
    aBuf = Join(aBuf, "")
    Set bStream = CreateObject("ADODB.Stream")
    bStream.Type = 1: bStream.Open
    With CreateObject("ADODB.Stream")
        .Type = 2: .Open: .WriteText aBuf
        .Position = 2: .CopyTo bStream: .Close
    End With

    bStream.SaveToFile FileName, 2: bStream.Close
    Set bStream = Nothing
End Sub
  • `Split("240,70,90,45,62,255,255", ",")`?.. Why do you need it to be `Variant`? What was the reason you could not create a big array? – GSerg Jun 03 '22 at 07:39
  • I can't use Split on one element like that. I have 15,000 of those lines that need to be done in a function automatically. It needs to be Variant to pass off into WriteBinary... – yeahbuddeh Jun 03 '22 at 16:17
  • How do the number of lines or the need to have a Variant contradict with using `Split`? – GSerg Jun 03 '22 at 16:23
  • can you please show me where i would use this Split? I edited my first post to show the function I need to pass off this array too. I cant use a large array, so i had to resort to an arraylist. Now these elements come in as strings ans I cant pass this arraylist off to WriteBinary. It needs single elements (like 240,70,90) not "240,70,90" – yeahbuddeh Jun 03 '22 at 16:29
  • I understand neither the problem nor your goal. I suspect you are confusing the number of arguments the `Array` *function* can accept and the number of elements *an array* can hold. [Here's](https://i.stack.imgur.com/NaTlE.png) an array with 500 million elements, it works (provided you have enough continuous RAM). Your `WriteBinary` also only requires a Variant because you made it that way, by not specifying the argument types. What is that you are [actually](https://meta.stackexchange.com/q/66377/147640) trying to do? – GSerg Jun 03 '22 at 16:40
  • I am basically creating a data file out of an array of bytes. I did not write the WriteBinary function since I am new to VBA, but have background in C, C++ and assembly. – yeahbuddeh Jun 03 '22 at 16:50
  • Creating a data file from an array of bytes involves declaring an array of bytes, opening a file `For Binary Access Write` and [`Put`ting](https://stackoverflow.com/a/9093957/11683) the array into the file. [Here's](https://stackoverflow.com/a/3364324/11683) the opposite procedure, reading a byte array from file. – GSerg Jun 03 '22 at 16:53
  • I know how to create the file using an array. I've done it. The issue is I can't declare a massive array. Try it, it won't work. Throws a syntax error. This file is 100,000 bytes. – yeahbuddeh Jun 03 '22 at 16:59
  • I just [showed](https://i.stack.imgur.com/NaTlE.png) you that 500000000 bytes works just fine. [Are you](https://stackoverflow.com/questions/72481327/large-buffer-not-allowed-in-vba7-1?noredirect=1#comment128059801_72481327) trying to pass 100000 arguments to the `Array` function, which it obviously cannot accept and which has very little to do with your ability to declare an array with 100000 elements? – GSerg Jun 03 '22 at 17:02
  • I believe you can definitely use a lot of elements but Yes that is the issue. How else can I declare it? – yeahbuddeh Jun 03 '22 at 17:11
  • Like [shown](https://i.stack.imgur.com/NaTlE.png)? `Dim arr() as byte : reDim arr(1 to whatever)`? Or directly `Dim arr(1 to whatever) as byte`, if you don't mind fixed size arrays? – GSerg Jun 03 '22 at 17:15
  • That's the problem. You can't use a massive array. The lines turn red and throws a syntax error. I could only use 24 line continuation underscores. – yeahbuddeh Jun 03 '22 at 17:28
  • You can't do `arr = Array(all my 100,000 bytes here)`. it wont work – yeahbuddeh Jun 03 '22 at 17:33
  • I didn't say you should do that, did I? In fact I kept saying the opposite, that you should not do that (i.e. attempt to call the `Array` *function* with obscene number of arguments). If you have 100000 bytes that you want to store in your source code, as numbers, then you must initialize the array per element. `arr(1) = 240`, `arr(2) = 70`. If you don't like that, come up with a different way of storing your numbers, such as in a file. Or indeed as comma-separated strings, in which case, you would use `Split` as suggested in the very first comment, to fill your array, element per element. – GSerg Jun 03 '22 at 17:41
  • The last part is where we are on the same page. I need to get every byte as an individual element. Im not sure how to use this Split function. Thats the whole point of these forums. Can you please show how I am supposed to use it? I have no idea how to go about doing that. I have an array of strings called `coll` as in the first post. How do I then use Split to achieve what I want? – yeahbuddeh Jun 03 '22 at 17:48
  • You have a `For` loop over your array of strings. Inside the loop, you call `Split` on the string and have a nested `For` loop over the array that the `Split` returns. Each element of that array you assign to the next unassigned element of your final byte array. You said you had a background in C, C++ and assembly, surely all three involve initializing an array with a `for` loop? – GSerg Jun 03 '22 at 17:54
  • Yes I have background in programming. for loops are easy. But syntax in VBA is very different. Just show me the line which the Split function is located. How does assignment look like? I can take it from there. Thank you. – yeahbuddeh Jun 03 '22 at 17:58
  • `arr = Split("240,70,90,45,62,255,255", ",")`?.. – GSerg Jun 03 '22 at 18:00
  • That will only assign a predetermined string to one array. I need to assign all the strings to the Ith element of my newly populated array. That is what I am looking for assistance with. – yeahbuddeh Jun 03 '22 at 18:12

0 Answers0