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
Printable View
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
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.
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:
Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click Dim Lrow As Integer Dim x As Integer Lrow = xlBlad.UsedRange.Rows.Count x = Lrow + 1 ' input data naar Excel xlBlad.Cells(x, 1) = monthcalender.SelectionStart xlBlad.Cells(x, 2) = txtaantalkm.Text xlBlad.Cells(x, 3) = txttijd.Text xlBlad.Cells(x, 4) = minkm.ToString xlBlad.Cells(x, 5) = kmperuur.ToString xlBlad.Cells(x, 6) = cbosoort.Text End Sub Private Sub Toevoegen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If strkeuze = "Ma" Then mstrBestand = Application.StartupPath & "\" & "looptijdenMA.xls" Else mstrBestand = Application.StartupPath & "\" & "looptijdenPA.xls" End If xlToepassing = xlGlobale.Application xlBook = xlToepassing.Workbooks.Open(mstrBestand) xlBlad = xlToepassing.ActiveSheet End Sub Private Sub Toevoegen_Unload(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closed xlToepassing.Quit() xlToepassing = Nothing xlBook = Nothing xlBlad = Nothing End Sub