Fast way to query listobject vba

坚强是说给别人听的谎言 提交于 2021-02-11 13:32:09

问题


I use excel 2016 and need to get a value from list object. There are several columns. For example three, and I need to get the value in column1 where column2=smth, column3=smth. How can I do this fast?


回答1:


If you want SQL syntax and speed in VBA then you need to use the ADO DB connector. In my experience it was 4x faster than using native Excel functions to find the data.

It's a lengthy topic that requires you read multiple articles. Much more information than should fit in to one SO post.

Here is an example article: https://technet.microsoft.com/en-us/library/ee692882.aspx

Here is the code from that example:

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:\Scripts\Test.xls;" & _
        "Extended Properties=""Excel 8.0;HDR=Yes;"";" 

objRecordset.Open "Select * FROM [Sheet1$] Where Number = 2", _
    objConnection, adOpenStatic, adLockOptimistic, adCmdText

Do Until objRecordset.EOF
    Wscript.Echo objRecordset.Fields.Item("Name"), _
        objRecordset.Fields.Item("Number")
    objRecordset.MoveNext
Loop

objRecordset.Close
objConnection.Close


来源:https://stackoverflow.com/questions/51091964/fast-way-to-query-listobject-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!