Results 1 to 3 of 3

Thread: [RESOLVED] create and read binary files

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    3

    Resolved [RESOLVED] create and read binary files

    All

    I am new in writing binary files and I encounter a problem while reading such file I just created in an Excel 2007 VBA-module.

    To start simple, I write all values in column A to a binary file. The data is alphanumeric so it can contain text, positive/negative numbers, a mix of text and numbers, ... The size is not fixed, so the number of characters vary.

    I use next code to write the binary file.

    Code:
        Dim intFileNum As Integer
        Dim strData As Variant
        
        Worksheets("Sheet1").Activate
        ActiveSheet.Range("a1").Activate
        intFileNum = FreeFile
        
        Open "C:\temp\test.dat" For Binary Access Write As intFileNum
        Do While ActiveCell.Value <> ""
            strData = ActiveCell.Value
            Put intFileNum, , strData
            ActiveCell.Offset(1, 0).Activate
        Loop
        Close intFileNum
    Next, I want to read the binary file and put the content on another sheet. This does not work as I only get empty cells
    Code:
        Dim intFileNum As Integer
        Dim strData As Variant
        
        Worksheets("Sheet2").Activate
        ActiveSheet.Range("a1").Activate
        intFileNum = FreeFile
        
        Open "C:\temp\test.dat" For Binary Access Read As intFileNum
        Do While Not EOF(intFileNum)
            Get intFileNum, , strData
            ActiveCell.Value = strData
            ActiveCell.Offset(1, 0).Activate
        Loop
        Close intFileNum
    Can someone help me?

    Regards
    Ino

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: create and read binary files

    i pasted your code and tested, it worked fine, of course cell formatting is not retained, so some variation in displayed results
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    3

    Re: create and read binary files

    For unknown reasons, the code does now work on my computer. Seems I had to restart my computer for some reason, although I must admit I am using some ADODB-connections to other XLS-sheets and it is known this might give unexpected memory/performance issues.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width