Results 1 to 19 of 19

Thread: [RESOLVED] C to Excel VBA

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    13

    Resolved [RESOLVED] C to Excel VBA

    Hi All,

    I want to use VBA to reading files with the following structure.

    Code:
    struct dathdr7 {
        u_short max_recs;	   
        u_short last_rec;	    
        char zeroes[24];
    };
    struct ctdata7 {
        float date;
        float open;
        float high;
        float low;
        float close;
        float volume;
        float op_int;
    };
    This is my VBA Code.

    VB Code:
    1. Option Explicit
    2.  
    3. Type head1
    4.      max_rec As Integer
    5.      last_rec As Integer
    6.      zeros As String * 24
    7. End Type
    8.  
    9. Type head2
    10.      ndate As Single
    11.      nopen As Single
    12.      nhigh As Single
    13.      nlow As Single
    14.      nclose As Single
    15.      nvolume As Single
    16.      nop_int As Single
    17. End Type
    18.  
    19. Sub test_read()
    20.  
    21. Dim inhandle As Integer
    22. Dim first_rec As head1
    23. Dim other_rec As head2
    24. Dim filenum As Long
    25. Dim out_ptr As Integer
    26.  
    27. out_ptr = 1
    28. filenum = FreeFile()
    29. Open "C:\Documents and Settings\Administrator\Desktop\Project\File.dat" For Binary As #filenum
    30.  
    31. Get #filenum, , first_rec
    32. Sheets("Sheet1").Range("A1").Offset(0, 0) = first_rec.max_rec
    33. Sheets("Sheet1").Range("A1").Offset(0, 1) = first_rec.last_rec
    34. Sheets("Sheet1").Range("A1").Offset(0, 2) = first_rec.zeros
    35.  
    36. While Not EOF(filenum)
    37.  
    38.     Get #filenum, , other_rec
    39.     Sheets("Sheet1").Range("A1").Offset(out_ptr, 0) = other_rec.ndate
    40.     Sheets("Sheet1").Range("A1").Offset(out_ptr, 1) = other_rec.nopen
    41.     Sheets("Sheet1").Range("A1").Offset(out_ptr, 2) = other_rec.nhigh
    42.     Sheets("Sheet1").Range("A1").Offset(out_ptr, 3) = other_rec.nlow
    43.     Sheets("Sheet1").Range("A1").Offset(out_ptr, 4) = other_rec.nclose
    44.     Sheets("Sheet1").Range("A1").Offset(out_ptr, 5) = other_rec.nvolume
    45.     Sheets("Sheet1").Range("A1").Offset(out_ptr, 6) = other_rec.nop_int
    46.     out_ptr = out_ptr + 1
    47.  
    48. Wend
    49.  
    50. Close #filenum
    51.  
    52. End Sub

    This problem I am having now is that the first record is OK. But the subsequent records, all read data are corrupted.

    Thanks in advance for all the helps.
    Last edited by RobDog888; May 7th, 2006 at 05:54 AM. Reason: Added [vbcode] tags

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