Transfer real time data from vb 2005 to excel

nico123

Member
Joined
Jul 21, 2009
Messages
7
Programming Experience
Beginner
hi,
I am working on a project that is able to display real time data from sensors from micro-controller. I am able to do so by sending the data through the serial port and then use vb 2005 to show the data. However, I want to save those data from vb 2005 to excel. How am I able to do it ? I am able to use vb to insert data to excel step by step, cell by cell using the following codes.

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim oExcel As Object
        Dim oBook As Object
        Dim oSheet As Object

        TextBox1.Text = "1"
        TextBox2.Text = "2"
        'Start a new workbook in Excel
        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.Workbooks.Add


        'Add data to cells of the first worksheet in the new workbook
        oSheet = oBook.Worksheets(1)
        oSheet.Range("A1").Value = "TextBox1"
        oSheet.Range("B1").Value = "TextBox2"
        oSheet.Range("A1:B1").Font.Bold = True

        oSheet.Range("A2").Value = TextBox1.Text
        oSheet.Range("B2").Value = TextBox2.Text

        'Save the Workbook and Quit Excel
        oBook.SaveAs("C:\exceltest.xls")
        oExcel.Quit()
    End Sub

Ok. Lets say TextBox1.Text and TextBox2.Text is my real time data. How do I save it to the excel sheet displaying the data in cell A2, A3, A4 when the time goes by. Eg. If I want to save TextBox1.Text to excel it would be, the data in the 1st seconds in cell A2, data in the 2nd seconds in A3, 3rd seconds A4, 4th seconds in A5, A6 and so on...
 
Ahm...string concatenation?

VB.NET:
oSheet.Range("A" & Me._rowCounter.ToString()).Value = TextBox1.Text
oSheet.Range("B" & Me._rowCounter.ToString()).Value = TextBox2.Text
Me._rowCounter += 1
 
VB.NET:
oSheet.Range("A" & Me._rowCounter.ToString()).Value = TextBox1.Text
oSheet.Range("B" & Me._rowCounter.ToString()).Value = TextBox2.Text
Me._rowCounter += 1

After inserting this code, i receive a blue underline on _rowCounter saying it's not a member in my file. How do I use or declare this _rowCounter ?
Sorry, I'm abit noob in this.
 
Declare it as Private variable in your form:

VB.NET:
Public Class HereStartsYourForm

      Private _rowCounter As Integer = 0 ' or any other default value you need

      ' ... here follows the rest of your code
 
Back
Top