I am using excel vba to read a binary file, then get the bytes to an array of bytes. However getting subscript out of range exception, in the line 13
Sub GetBinaryData()
Dim aryBytes() As Byte
Dim bytInput As Byte
Dim intFileNumber
Dim intFilePos
intFileNumber = FreeFile
Open "Binary.bin" For Binary As #intFileNumber
intFilePos = 1
While Not EOF(intFileNumber)
Get #intFileNumber, intFilePos, bytInput
aryBytes(intFilePos) = bytInput
ReDim aryBytes(UBound(aryBytes) + 1)
intFilePos = intFilePos + 1
Wend
Close #intFileNumber
End Sub
It fails as on the 1st trip through the loop
aryBytes(intFilePos)tries to access element 1 ofaryByteswhich does not exist (its declared dynamic & there is no preceedingReDim)Also EOF wont behave as you expect for binary access, try LOF instead
Here is a way to do it byte-by-byte;
You can also do it in a single read;