Results 1 to 4 of 4

Thread: Transferring data to Excel

Threaded View

  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.

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