ExecuteExcel4Macro从已关闭的工作簿中获取值我找到了这段代码,并认为如果我只需要从一个封闭的表中提取一个值,那么使用它可能会很好。strInfoCell = "'" & strPath & "[" & strFile & "]Sheet1'!R3C3"myvalue = ExecuteExcel4Macro(strInfoCell)当我运行此代码,我得到一个值strinfocell的'C:\Users\my.name\Desktop[QOS DGL stuff.xlsx]Sheet1'!R3C3但是当我运行代码时会弹出一个对话框,显示带有“ QOS DGL suff”显示的桌面文件。造成这种情况的原因是什么,不仅仅是按预期撤回数据?我知道路径和文件名是正确的,因为如果我从调试输出中复制它们并将它们粘贴到start>>run那么打开正确的工作表。我知道Sheet1(名为:) ACL,确实有价值cells(3,3)
3 回答
qq_遁去的一_1
TA贡献1725条经验 获得超7个赞
类似的应用程序,但没有上面示例中的硬编码路径。此函数从另一个已关闭的工作簿复制值,类似于= INDIRECT()函数,但不复杂。这只返回值...而不是引用。因此它不能与需要引用的其他函数一起使用(即:VLOOKUP())。将此代码粘贴到新的VBA模块中:
'Requires filename, sheetname as first argument and cell reference as second argument'Usage: type in an excel cell -> =getvalue(A1,B1)'Example of A1 -> C:\TEMP\[FILE1.XLS]SHEET1''Example of B1 -> B3'This will fetch contents of cell (B3) located in (sheet1) of (c:\temp\file1.xls)'Create a module and paste the code into the module (e.g. Module1, Module2)Public xlapp As ObjectPublic Function getvalue(ByVal filename As String, ref As String) As Variant' Retrieves a value from a closed workbook Dim arg As String Dim path As String Dim file As String filename = Trim(filename) path = Mid(filename, 1, InStrRev(filename, "\")) file = Mid(filename, InStr(1, filename, "[") + 1, InStr(1, filename, "]") - InStr(1, filename, "[") - 1) If Dir(path & file) = "" Then getvalue = "File Not Found" Exit Function End If If xlapp Is Nothing Then 'Object must be created only once and not at each function call Set xlapp = CreateObject("Excel.application") End If ' Create the argument arg = "'" & filename & "'!" & Range(ref).Range("A1").Address(, , xlR1C1) 'Execute an XLM macro getvalue = xlapp.ExecuteExcel4Macro(arg)End Function
MYYA
TA贡献1868条经验 获得超4个赞
上面的代码
strInfoCell = "'" & strPath & "[" & strFile & "]Sheet1'!R3C3"myvalue = ExecuteExcel4Macro(strInfoCell)
应该读
strInfoCell = "'" & strPath & "[" & strFile & "]" & "Sheet1'!R3C3"myvalue = ExecuteExcel4Macro(strInfoCell)
它缺少“&”
不需要功能
- 3 回答
- 0 关注
- 1291 浏览
相关问题推荐
添加回答
举报
0/150
提交
取消