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.
Regards,
Barklie Estes
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.
Field1 | Field2 |
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