'in the "general declarations" section of the form:
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oWS As Excel.Worksheet
Dim oRng As Excel.Range
Dim lOutputRow as Long
Private Sub Form_Load()
' Start Excel and get Application object.
Set oXL = CreateObject("Excel.Application")
' Create a new workbook.
Set oWB = oXL.Workbooks.Add
Set oWS = oWB.ActiveSheet
'Set the headings ** change this as apt! **
oWS.Cells(1,1) = "Header1"
oWS.Cells(1,2) = "Header2"
oWS.Rows(1).Font.Bold = True
'Save the workbook ** you should have error checking here, in case this file exists already **
oWB.SaveAs "C:\Book1.xls"
'Set the row to put the next set of data into
lOutputRow = 2
End Sub
Private Sub Form_UnLoad()
'Save and close the Workbook
oWB.Save
oWB.Close
Set oWB = Nothing
'Quit Excel
oXL.Quit
Set oXL = Nothing
End Sub
Private Sub Timer1_Timer()
' Read in and display values.
Dim in_byte As String
Dim in_message As String
in_message = ""
MSComm1.InputLen = 1 ' Read a single byte from buffer each time.
MSComm1.Output = "READ?" & Chr(13) & Chr(10) ' Query instrument.
Do
Do
DoEvents ' Continue processing events.
Loop Until MSComm1.InBufferCount >= 1 ' Wait for a character.
in_byte = MSComm1.Input
in_message = in_message & in_byte ' Append to i/p string.
Loop Until (in_byte = Chr(10)) ' Wait for a LF terminator.
Label2.Caption = in_message ' Display message.
'insert values into Excel
oWS.Cells(lOutputRow, 1) = (lOutputRow-2)*5
oWS.Cells(lOutputRow, 2) = in_message
lOutputRow = lOutputRow + 1
'save after every 10 lines
If lOutputRow Mod 10 = 0 Then oWB.Save
If lOutputRow > 65536 Then
stop '** you cannot put in more values, so you need to create a new WorkSheet/WorkBook as you see fit **
End If
End Sub