VLookup Using .GetRows Output

Barklie

Board Regular
Joined
Jul 4, 2013
Messages
86
Greetings,

I am using a VBA Script to retrieve a record set from a closed workbook using the .GetRows method. However, it appears the output is contaminated as I am unable to use a VLookup formula on it. Pasting it into Excel and then recopying it is the workaround that I'm using now, which does fine. I'm mainly just frustrated/curious because I don't understand how an 8024 variable could work with VLookup in all instances except this one. Sample Data and Code are below. I'm sure it's something fundamental that I don't understand.


Field1Field2
a
5​
b
15​
c
25​


VBA Code:
Sub LookupAHJ()

'Dimension variables
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

'Assign variables
Path = "C:\Users\beste\OneDrive\Desktop 1\Sample Source.xlsx"
Find = "b"
myVar = 5
Set cn = New ADODB.Connection

'Retrieve recordset
Set cn = New ADODB.Connection
cn.ConnectionString = _
    "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=" & Path & ";" & _
    "Extended Properties='Excel 12.0 Xml;HDR=YES';"
cn.Open
Set rs = New ADODB.Recordset
rs.ActiveConnection = cn
rs.Source = "SELECT * FROM MyNamedRange"
rs.Open
DataTable = rs.GetRows

'Test that DataTable was retrieved successfully
If DataTable(1, 1) <> "15" Then MsgBox ("Did not get table successfully")

'Find value
MsgBox (Application.VLookup(Find, DataTable, 2, False))

'Close Connection
rs.Close
cn.Close

End Sub


Regards,
Barklie Estes
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,223,880
Messages
6,175,154
Members
452,615
Latest member
bogeys2birdies

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