Set ObjWB = Workbooks.Open("c:\Test.xlsx")
如果我使用workbooks.open
命令,则会打开该Excel工作簿。
我需要在不打开Excel工作簿的情况下读取单元格值。
答案 0 :(得分:0)
在GetValue
(工作簿和工作表)中设置参数后运行子ReadFromClosedWorkbook
。您可以将其中一个或两个作为参数传递给调用过程。
Sub GetValue()
Debug.Print ReadFromClosedWorkbook("A1")
End Sub
Private Function ReadFromClosedWorkbook(Target As String) As Variant
Const WbFullName = "D:\My Documents\Your file name.xlsx"
Dim PathName As String
Dim WbName As String
Dim WsName As String
Dim Target As String
Dim Sp() As String
WsName = "My Worksheet's Name"
Sp = Split(WbFullName, "\")
WbName = Sp(UBound(Sp))
ReDim Preserve Sp(UBound(Sp) - 1)
PathName = Join(Sp, "\") & "\"
If Len(Dir(WbFullName)) Then
Target = "'" & PathName & _
"[" & WbName & "]" & WsName & _
"'!" & Range(Target).Address(True, True, xlR1C1)
ReadFromClosedWorkbook = ExecuteExcel4Macro(Target)
End If
End Function