Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9248429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:56:35+00:00 2026-06-18T09:56:35+00:00

I am using excel vba to read a binary file, then get the bytes

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:56:36+00:00Added an answer on June 18, 2026 at 9:56 am

    It fails as on the 1st trip through the loop aryBytes(intFilePos) tries to access element 1 of aryBytes which does not exist (its declared dynamic & there is no preceeding ReDim)

    Also EOF wont behave as you expect for binary access, try LOF instead

    Here is a way to do it byte-by-byte;

    Open "Binary.bin" For Binary Access Read As #intFileNumber
      '//only need to dimension once
      ReDim aryBytes(LOF(intFileNumber) - 1)
      '//easier to count from zero
      intFilePos = 0
      While intFilePos < LOF(intFileNumber) '//limit to Length-Of-File
        Get #intFileNumber, intFilePos + 1, bytInput
          aryBytes(intFilePos) = bytInput
          intFilePos = intFilePos + 1
        Wend
    Close #intFileNumber
    

    You can also do it in a single read;

    Open "Binary.bin" For Binary Access Read As #intFileNumber
      ReDim aryBytes(LOF(intFileNumber) - 1)
      Get #intFileNumber, , aryBytes
    Close #intFileNumber
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read web pages using Excel VBA. How do I carry out
How to get the size of the posters by using vba excel. I am
I am looking to run SQL queries using VBA code in an Excel file.
I'm trying to fill an array with consecutive integers using Excel and VBA to
I need to convert a simple string in a Byte array using excel VBA.
I am trying to get the table boundaries of an excel sheet using VBA.
I'm having trouble with changing the file access mode on Excel workbooks using VBA.
I'm using VBA to save an excel worksheet as a CSV file. The macro
I'm using excel VBA. I want to press a button that opens another file
I'm using VBA in Excel to read a spreadsheet and modify its contents. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.