|
-
Nov 5th, 2012, 09:40 AM
#4
Thread Starter
New Member
entering data in excel sheet trough Vb application form
I did following code, ad this is pretty what i need, but i need more control over it,
actually i need ID, i meant to put another text box and to link it with column F for instance,
this will provide me back info about row which application is writing to. Because i don't know which row
is already taken, back info regarding ID is required.
any idea how to read that column and display next empty ID row?
Thanks in advance
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet
Dim lastRow As Long
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open("C:\file.xlsx")
xlWorksheet = xlWorkBook.Worksheets(1)
xlApp.Visible = False
lastRow = xlWorksheet.Range("A" & xlApp.Rows.CountLarge).End(Excel.XlDirection.xlUp).Row + 1
With xlWorksheet
.Range("A" & lastRow).Value = Me.TextBox1.Text
.Range("B" & lastRow).Value = Me.TextBox2.Text
.Range("C" & lastRow).Value = Me.TextBox3.Text
.Range("D" & lastRow).Value = Me.TextBox4.Text
.Range("E" & lastRow).Value = Me.TextBox5.Text
.Range("F" & lastRow).Value = Me.TextBox6.Text
End With
xlWorkBook.Save()
xlWorkBook.Close()
releaseObject(xlWorkBook)
releaseObject(xlWorksheet)
xlApp.Quit()
releaseObject(xlApp)
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'clears text boxes
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
End Sub
Last edited by zoomtronic; Nov 5th, 2012 at 11:49 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|