Putting lots of values into an array quickly

bluto32

New Member
Joined
Jan 5, 2011
Messages
37
I have a bunch of 50 bytes I would like to write to a file in VBA.

One way of doing this is to enter them in separate cells on a worksheet (say A1 to A50), and write a loop in VBA to write each one to the file.

However, I would rather not have the data on a worksheet in the first place. My plan is to put these 50 bytes into an array using VBA directly, and then write a loop to write each byte to the file.

Something like:

Code:
Dim myarray(50) As Byte
Dim n As Integer
 
[COLOR=red]myarray = (1, 4 ,29, 35, 246, [I]...more bytes in here...[/I], 8)[/COLOR]
 
Open "file" For Binary As #1
For n = 1 to 50
     Put #1 , , myarray(n)
Next n

The red line is the one which I can't fix. Is there a way of populating an array quickly i.e. in one or two lines? (I could of course populate the array from the worksheet, but then there's no point using an array any more... :biggrin:)

I used to have a ZX Spectrum (those were the days!!!), and there I would type in:

Data 1, 4, 29, 35, ...

and use the Read statement to pick out the numbers one by one.

Any ideas, apart from using a string instead, and messing about with delimeters?

Thanks,
Bluto
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
AFAIK, VBA doesn't allow you create an array exactly like that. However, using the Split function and a Variant variable, you can get pretty close to what you want..
Code:
Sub FillArray()
    Dim myarray                         As Variant
    Dim n                               As Integer
    myarray = Split("1, 4 ,29, 35, 246, 8", ",")
    Open "file" For Binary As #1
    For n = LBound(myarray) To UBound(myarray)
        'need to convert from string to byte...
        Put #1, , cbyte(myarray(n))
    Next n
End Sub

of course, the other option would be to populate the array 'one at a time'

Code:
myarray(0) = 1
myarray(1) = 4
etc
and create those statements with a copy paste from formulas in a worksheet.

Excel Workbook
AB
11Myarray(0) = 1
24Myarray(1) = 4
329Myarray(2) = 29
455Myarray(3) = 55
Sheet3
Excel 2007
Cell Formulas
RangeFormula
B1="Myarray(" & ROW()-1 & ") = " & A1
 
Upvote 0
Thank you, Chris - that's very informative. I shall investigate the Split function - haven't used it yet - and remember to put the CByte in next time.

Your second method is really sneaky - using the worksheet only temporarily to aid entering the VBA code quickly!

Much appreciated,
Bluto
 
Upvote 0
Unless I'm missing something, the array function should suit your purposes.

Code:
myarray = Array(1, 4 ,29, 35, 246, ...more bytes in here..., 8)
 
Upvote 0
Thanks, Greg. This has done the trick after a bit of experimenting.
I couldn't get this to work initially:

Code:
Sub test()
  Dim myarray(5) as Integer
  [COLOR=red]myarray = Array(1, 3, 5, 7, 9)       [/COLOR][COLOR=magenta]'gives error "Can't assign to array"[/COLOR]
  Range("A1") = myarray(4)
End Sub

After a bit of googling, I found that it does work if myarray is declared as a variant (but not an array with variant elements):

Code:
Sub test()
  [COLOR=green]Dim myarray As Variant[/COLOR]
  myarray = Array(1, 3, 5, 7, 9)
  Range("A1") = myarray(4)
End Sub

A side effect of this is that the value 9 is put into "A1", not 7!!!

It seems that the positions of the elements are numbered 0 to 4 rather than the 1 to 5 I was expecting.

VBA is terrific fun, but sure is confusing sometimes!

Many thanks for helping me find a solution.
Bluto
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top