Hi. I'm trying to use Excels Vlookup function to populate a text box within a form. I want to enter a number, look up this number in a named range and return the corresponding data into the second text box. I've got so far but my knowledge has come to its limit, and the code below doesn't work :unhappy: Please could someone point me in the right direction?
The workbook is named 'Database2.xlsx' it lives on the 'C:' drive. The sheet with the data is 'Sheet1' and the named range is 'Table1'
Thanks,
Jon
The workbook is named 'Database2.xlsx' it lives on the 'C:' drive. The sheet with the data is 'Sheet1' and the named range is 'Table1'
Thanks,
Jon
VB.NET:
Imports System.Data
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim conn As System.Data.OleDb.OleDbConnection
Dim da As System.Data.OleDb.OleDbDataAdapter
Dim xlApp As Excel.Application
Dim xlAddy As String = "C:\Database2.xlsx"
Dim VlResult As VariantType
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetData.Click
If txtId.Text = "" Then
Return
End If
xlBook = xlApp.Workbooks("C:\Database2.xlsx")
xlSheet = xlBook.Worksheets("Sheet1")
xlRange.Name = ("Table1")
Dim id As VariantType
id = txtId.Text
Try
With xlRange
VlResult = xlApp.VLOOKUP(id & ", Table1, 2 , 0")
End With
Catch ex As Exception
End Try
txtName.Text = VlResult
End Sub