Reading a workbooks without opening it with ADO

后端 未结 2 1876
孤城傲影
孤城傲影 2021-01-07 12:46

After this question: get value/charts in another workbooks without opening it

I have coded this:

Sub test()

Dim oConn As New ADODB.Connection
Dim r         


        
2条回答
  •  醉酒成梦
    2021-01-07 13:03

    My solution:

    Function GetValue()
    
    Path = "C:\Path\"
        File = "Doc.xlsm"
        Sheet = "Sheet_name"
        Ref = "D4"
    
         'Retrieves a value from a closed workbook
        Dim Arg As String
         'Make sure the file exists
       If Right(Path, 1) <> "\" Then Path = Path & "\"
       If Dir(Path & File) = "" Then
           GetValue = "File not  Found"
           Exit Function
        End If
         'Create the argument
        Arg = "'" & Path & "[" & File & "]" & CStr(Sheet) & "'!" & Range(Ref).Range("A1").Address(, , xlR1C1)
         'Check the value
    
         MsgBox Arg
    
        'Execute XML
    
        GetValue = ExecuteExcel4Macro(Arg)
    End Function
    

    It has the advantage of not using complex adodb connection, but may be less powerfull.

提交回复
热议问题