vlookup in vb.net in retriving data

GajendraSantosh

New Member
Joined
Jul 26, 2017
Messages
1
Hello!.
i am trying to retrieve information from .xlsx workbook to vb.net userform using vlookup function . iam not getting the output . where i am wrong would you please guide .
as i am new to .net & vba .

Dim r1, r2, r3, r4, r5, r6 As Long
Dim rng As Range
rng = xlWorkBook.Worksheets(2).Range("A:H")
P1.Text = xlWorkBook.Worksheets(2).Application.VLookup("P1_" & txtRefNo.Text, rng, 1, False)
P2.Text = xlWorkBook.Worksheets(2).Application.VLookup("P2_" & txtRefNo.Text, rng, 1, False)
P3.Text = xlWorkBook.Worksheets(2).Application.VLookup("P3_" & txtRefNo.Text, rng, 1, False)
P4.Text = xlWorkBook.Worksheets(2).Application.VLookup("P4_" & txtRefNo.Text, rng, 1, False)
P5.Text = xlWorkBook.Worksheets(2).Application.VLookup("P5_" & txtRefNo.Text, rng, 1, False)
P6.Text = xlWorkBook.Worksheets(2).Application.VLookup("P6_" & txtRefNo.Text, rng, 1, False)


'On Error Resume Next
If P1.Text = "P1_" & txtRefNo.Text Then
r1 = xlWorkBook.Application.Match(Val(P1.Text), xlWorkBook.Worksheets(2).Columns(1), 0)
txtPDate1.Text = xlWorkBook.Worksheets(2).Range("B" & r1).Value
txtPCash1.Text = xlWorkBook.Worksheets(2).Range("C" & r1).Value
txtPBank1.Text = xlWorkBook.Worksheets(2).Range("D" & r1).Value
txtPChqNo1.Text = xlWorkBook.Worksheets(2).Range("E" & r1).Value
txtPTotal1.Text = xlWorkBook.Worksheets(2).Range("F" & r1).Value
cmbPStatus1.Text = xlWorkBook.Worksheets(2).Range("G" & r1).Value
txtPRemarks1.Text = xlWorkBook.Worksheets(2).Range("H" & r1).Value
End If


'On Error Resume Next
If P2.Text = "P2_" & txtRefNo.Text Then
r2 = xlWorkBook.Application.Match(P2, xlWorkBook.Worksheets(2).Columns(1), 0)
txtPDate2.Text = xlWorkBook.Worksheets(2).cells(r2, "B").Value
txtPCash2.Text = xlWorkBook.Worksheets(2).cells(r2, "C").Value
txtPBank2.Text = xlWorkBook.Worksheets(2).cells(r2, "D").Value
txtPChqNo2.Text = xlWorkBook.Worksheets(2).cells(r2, "E").Value
txtPTotal2.Text = xlWorkBook.Worksheets(2).cells(r2, "F").Value
cmbPStatus2.Text = xlWorkBook.Worksheets(2).cells(r2, "G").Value
txtPRemarks2.Text = xlWorkBook.Worksheets(2).cells(r2, "H").Value
End If


'On Error Resume Next
If P3.Text = "P3_" & txtRefNo.Text Then
r2 = xlWorkBook.Worksheets(2).Application.WorksheetFunction.Match(P3, xlWorkBook.Worksheets(2).Columns(1), 0)
txtPDate3.Text = xlWorkBook.Worksheets(2).cells(r3, "B").Value
txtPCash3.Text = xlWorkBook.Worksheets(2).cells(r3, "C").Value
txtPBank3.Text = xlWorkBook.Worksheets(2).cells(r3, "D").Value
txtPChqNo3.Text = xlWorkBook.Worksheets(2).cells(r3, "E").Value
txtPTotal3.Text = xlWorkBook.Worksheets(2).cells(r3, "F").Value
cmbPStatus3.Text = xlWorkBook.Worksheets(2).cells(r3, "G").Value
txtPRemarks3.Text = xlWorkBook.Worksheets(2).cells(r3, "H").Value
End If
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top