VBA - Close workbook after acquiring necessary information

前端 未结 2 1621
太阳男子
太阳男子 2021-01-15 18:27

Your assistance is required on solving this world (Excel VBA) problem. I am using a VBA to populate an immense workbook (500 Cells per row), from a bucket load of workbooks(

2条回答
  •  隐瞒了意图╮
    2021-01-15 18:44

    Well how about querying the excel files using ADO instead?

    Function getField(Path As String, WorksheetName As String, CellRange As String) As Variant
        Const adOpenStatic = 3
        Const adLockOptimistic = 3
        Const adCmdText = &H1
    
        Set objConnection = CreateObject("ADODB.Connection")
        Set objRecordset = CreateObject("ADODB.Recordset")
    
        objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & Path & ";" & _
                "Extended Properties=""Excel 8.0;HDR=NO;"";"
    
        objRecordset.Open "Select F" & Range(CellRange).Column & " as Val  FROM [" & WorksheetName & "$]", _
            objConnection, adOpenStatic, adLockOptimistic, adCmdText
    
        objRecordset.Move Range(CellRange).Row - 1
    
        getField = objRecordset("Val")
    
        objRecordset.Close
        objConnection.Close
    End Function
    

提交回复
热议问题