[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:
Option Explicit
Type head1
max_rec As Integer
last_rec As Integer
zeros As String * 24
End Type
Type head2
ndate As Single
nopen As Single
nhigh As Single
nlow As Single
nclose As Single
nvolume As Single
nop_int As Single
End Type
Sub test_read()
Dim inhandle As Integer
Dim first_rec As head1
Dim other_rec As head2
Dim filenum As Long
Dim out_ptr As Integer
out_ptr = 1
filenum = FreeFile()
Open "C:\Documents and Settings\Administrator\Desktop\Project\File.dat" For Binary As #filenum
Get #filenum, , first_rec
Sheets("Sheet1").Range("A1").Offset(0, 0) = first_rec.max_rec
Sheets("Sheet1").Range("A1").Offset(0, 1) = first_rec.last_rec
Sheets("Sheet1").Range("A1").Offset(0, 2) = first_rec.zeros
While Not EOF(filenum)
Get #filenum, , other_rec
Sheets("Sheet1").Range("A1").Offset(out_ptr, 0) = other_rec.ndate
Sheets("Sheet1").Range("A1").Offset(out_ptr, 1) = other_rec.nopen
Sheets("Sheet1").Range("A1").Offset(out_ptr, 2) = other_rec.nhigh
Sheets("Sheet1").Range("A1").Offset(out_ptr, 3) = other_rec.nlow
Sheets("Sheet1").Range("A1").Offset(out_ptr, 4) = other_rec.nclose
Sheets("Sheet1").Range("A1").Offset(out_ptr, 5) = other_rec.nvolume
Sheets("Sheet1").Range("A1").Offset(out_ptr, 6) = other_rec.nop_int
out_ptr = out_ptr + 1
Wend
Close #filenum
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.