Results 1 to 4 of 4

Thread: Transferring data to Excel

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    33

    Transferring data to Excel

    I am writing a program which transfers data from the serial port to an Excel file.

    At the moment, values are read into a label every 5 seconds. Each new value overwrites the previous one so there is only one value in the label at a time.
    Below is the code which does this.

    VB Code:
    1. Private Sub Timer1_Timer()
    2. ' Read in and display values.
    3.     Dim in_byte As String
    4.     Dim in_message As String
    5.     in_message = ""
    6.     MSComm1.InputLen = 1    ' Read a single byte from buffer each time.
    7.     MSComm1.Output = "READ?" & Chr(13) & Chr(10)   ' Query instrument.
    8.     Do
    9.       Do
    10.         DoEvents    ' Continue processing events.
    11.         Loop Until MSComm1.InBufferCount >= 1   ' Wait for a character.
    12.         in_byte = MSComm1.Input
    13.         in_message = in_message & in_byte    ' Append to i/p string.
    14.     Loop Until (in_byte = Chr(10))          ' Wait for a LF terminator.
    15.     Label2.Caption = in_message   ' Display message.
    16. End Sub

    I now need to send these values to an Excel file starting at cell B3. The times must start at cell A3, the first time being 0 and incrementing in steps of five.
    The reason that they must start here is that later i will be inserting headings in the range A1 to B2 with a VB macro.

    I already have a program which creates a two dimensional array using a random number generator and sends it to an Excel file.
    The following code does this.

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim oXL As Excel.Application
    3.     Dim oWB As Excel.Workbook
    4.     Dim oWS As Excel.Worksheet
    5.     Dim oRng As Excel.Range
    6.    
    7. ' Start Excel and get Application object.
    8.     Set oXL = CreateObject("Excel.Application")
    9.      
    10. ' Get a new workbook.
    11.     Set oWB = oXL.Workbooks.Add
    12.     Set oWS = oWB.ActiveSheet
    13.  
    14. ' Initialize the random number generator.
    15.     Randomize
    16.  
    17. ' Create an array with 100 rows and 2 columns.
    18.     Dim DataArray(1 To 100, 1 To 2) As Variant
    19.     Dim r As Integer
    20.     For r = 1 To 100
    21.        DataArray(r, 1) = r - 1
    22.        DataArray(r, 2) = Rnd() * 1000
    23.     Next
    24.    
    25. ' Transfer the array to the worksheet starting at cell A3.
    26.     Range("A3").Resize(100, 2).Value = DataArray
    27.    
    28. 'Save the Workbook and Quit Excel
    29.    oWB.SaveAs "C:\Book1.xls"
    30.    oXL.Quit

    What i need to do is replace the random number generator with the values from the serial port and there corresponding times.

    The program must be fully automated and be able to cater for an unknown number of values.

    Can anyone help?
    Last edited by royshh; May 15th, 2005 at 10:52 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Transferring data to Excel

    It will be slow to continually open and close the workbook, so I would recommend keeping it open for the life of the program, with an occasional Save to make sure you don't lose too much data if your computer crashes.

    You should always close objects when you have finished using them, and then set them to nothing (see my code in Form_Unload).
    VB Code:
    1. 'in the "general declarations" section of the form:
    2. Dim oXL As Excel.Application
    3. Dim oWB As Excel.Workbook
    4. Dim oWS As Excel.Worksheet
    5. Dim oRng As Excel.Range
    6. Dim lOutputRow as Long
    7.  
    8.  
    9. Private Sub Form_Load()
    10.    
    11. ' Start Excel and get Application object.
    12.     Set oXL = CreateObject("Excel.Application")
    13.      
    14. ' Create a new workbook.
    15.     Set oWB = oXL.Workbooks.Add
    16.     Set oWS = oWB.ActiveSheet
    17.  
    18. 'Set the headings  ** change this as apt! **
    19.     oWS.Cells(1,1) = "Header1"
    20.     oWS.Cells(1,2) = "Header2"
    21.     oWS.Rows(1).Font.Bold = True
    22.  
    23. 'Save the workbook  ** you should have error checking here, in case this file exists already **
    24.     oWB.SaveAs "C:\Book1.xls"
    25.  
    26. 'Set the row to put the next set of data into
    27.     lOutputRow = 2
    28.  
    29. End Sub
    30.  
    31.  
    32. Private Sub Form_UnLoad()    
    33.  
    34. 'Save and close the Workbook
    35.    oWB.Save
    36.    oWB.Close
    37.    Set oWB = Nothing
    38.  
    39. 'Quit Excel
    40.    oXL.Quit
    41.    Set oXL = Nothing
    42.  
    43. End Sub
    44.  
    45.  
    46. Private Sub Timer1_Timer()
    47. ' Read in and display values.
    48.     Dim in_byte As String
    49.     Dim in_message As String
    50.     in_message = ""
    51.     MSComm1.InputLen = 1    ' Read a single byte from buffer each time.
    52.     MSComm1.Output = "READ?" & Chr(13) & Chr(10)   ' Query instrument.
    53.     Do
    54.       Do
    55.         DoEvents    ' Continue processing events.
    56.       Loop Until MSComm1.InBufferCount >= 1   ' Wait for a character.
    57.       in_byte = MSComm1.Input
    58.       in_message = in_message & in_byte    ' Append to i/p string.
    59.     Loop Until (in_byte = Chr(10))          ' Wait for a LF terminator.
    60.     Label2.Caption = in_message   ' Display message.
    61.  
    62. 'insert values into Excel
    63.     oWS.Cells(lOutputRow, 1) = (lOutputRow-2)*5
    64.     oWS.Cells(lOutputRow, 2) = in_message
    65.     lOutputRow = lOutputRow + 1
    66. 'save after every 10 lines
    67.     If lOutputRow Mod 10 = 0 Then oWB.Save
    68.     If lOutputRow > 65536 Then
    69. stop  '** you cannot put in more values, so you need to create a new WorkSheet/WorkBook as you see fit **
    70.     End If
    71.  
    72. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    33

    Re: Transferring data to Excel

    I tried your code but it is giving an error on the line
    VB Code:
    1. Private Sub Form_UnLoad()

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Transferring data to Excel

    If you get an error that you want help with, you should not only specify where the error occured, but what the error was too.

    In this case it was a minor mistake on my part, that line should have been:
    VB Code:
    1. Private Sub Form_Unload(Cancel As Integer)

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