Programming for Everybody: July 2026

Programming in Visual Basic.Net: Get data from Excel file in VB.NET

 



Full Code

Imports ClosedXML.Excel
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Using workb As New XLWorkbook("H:\Names2.xlsx")
Dim worksh = workb.Worksheet("Sheet4")
Dim lastrow As Integer = worksh.LastRowUsed.RowNumber()
Dim idfound As Boolean = False
For i As Integer = 2 To lastrow
If worksh.Cell(i, 1).GetValue(Of Integer) = CInt(TextBox1.Text) Then
TextBox2.Text = worksh.Cell(i, 2).GetString()
TextBox3.Text = worksh.Cell(i, 3).GetString()
TextBox4.Text = worksh.Cell(i, 4).GetString()
idfound = True
Exit For
End If
Next
If Not idfound Then
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
End If
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class