Results 1 to 3 of 3

Thread: Writing to Excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    7

    Writing to Excel

    I'm doing a project and I need to write data from textboxes to an Excel file. There is already data in the Excel file, but how can I check which row is the first empty one and then write the data to that row.

    greetz

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Writing to Excel

    If you can assume that each row in column x has data, such that the first empty one is the one you want to write in, you can loop through the cells in that column (I think that would be cells(rowNumber,colNumber), but I can't check it right now) looking for the empty one.

    However, this is best if it only happens once. If you will be writing many times, it would be best to keep the next row to write to (or last one written to) as a variable in the program. Figure it out once, then manage it in code.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    7

    Re: Writing to Excel

    This code I used, but somehow it only worked once. Afterwords I did some changes, maybe that has been the problem.
    And I've got another question, how could you automaticly save the changes you make in the excel file.

    VB Code:
    1. Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
    2.  
    3.         Dim Lrow As Integer
    4.         Dim x As Integer
    5.  
    6.         Lrow = xlBlad.UsedRange.Rows.Count
    7.         x = Lrow + 1
    8.  
    9.         ' input data naar Excel
    10.         xlBlad.Cells(x, 1) = monthcalender.SelectionStart
    11.         xlBlad.Cells(x, 2) = txtaantalkm.Text
    12.         xlBlad.Cells(x, 3) = txttijd.Text
    13.         xlBlad.Cells(x, 4) = minkm.ToString
    14.         xlBlad.Cells(x, 5) = kmperuur.ToString
    15.         xlBlad.Cells(x, 6) = cbosoort.Text
    16.  
    17. End Sub
    18.  
    19. Private Sub Toevoegen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    20.  
    21.         If strkeuze = "Ma" Then
    22.             mstrBestand = Application.StartupPath & "\" & "looptijdenMA.xls"
    23.         Else
    24.             mstrBestand = Application.StartupPath & "\" & "looptijdenPA.xls"
    25.         End If
    26.  
    27.         xlToepassing = xlGlobale.Application
    28.         xlBook = xlToepassing.Workbooks.Open(mstrBestand)
    29.         xlBlad = xlToepassing.ActiveSheet
    30.  
    31. End Sub
    32.  
    33. Private Sub Toevoegen_Unload(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closed
    34.  
    35.         xlToepassing.Quit()
    36.         xlToepassing = Nothing
    37.         xlBook = Nothing
    38.         xlBlad = Nothing
    39.  
    40. End Sub

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