【问题标题】:ExecuteExcel4Macro to get value from closed workbookExecuteExcel4Macro 从关闭的工作簿中获取价值
【发布时间】:2012-03-04 19:35:04
【问题描述】:

我发现了这段代码,并认为如果我只需要从封闭的工作表中提取一个值,它可能会很好用。

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) 中确实有一个值

【问题讨论】:

  • ExecuteExcel4Macro 应该做什么?显然它只是一个用于运行 Excel 4.0 宏的函数。
  • 所以是的,它应该将 strinfocell 作为宏执行。这反过来应该返回所提供文件夹中工作簿“QOS DGL Stuff”中单元格 C3 的值。
  • myvalue 的类型是什么?

标签: excel vba excel-4.0


【解决方案1】:

这取决于你如何使用它。正在向您显示打开文件对话框,因为“strPath”最后没有“”;)

试试这个代码。

Option Explicit

Sub Sample()
    Dim wbPath As String, wbName As String
    Dim wsName As String, cellRef As String
    Dim Ret As String
    
    'wbPath = "C:\Documents and Settings\Siddharth Rout\Desktop\"
    wbPath = "C:\Users\my.name\Desktop\"
    
    wbName = "QOS DGL stuff.xls"
    wsName = "ACL"
    cellRef = "C3"
    
    Ret = "'" & wbPath & "[" & wbName & "]" & _
          wsName & "'!" & Range(cellRef).Address(True, True, -4150)
    
    MsgBox ExecuteExcel4Macro(Ret)
End Sub

【讨论】:

    【解决方案2】:

    上面的代码

    strInfoCell = "'" & strPath & "[" & strFile & "]Sheet1'!R3C3"
    
    myvalue = ExecuteExcel4Macro(strInfoCell)
    

    应该阅读

    strInfoCell = "'" & strPath & "[" & strFile & "]" & "Sheet1'!R3C3"
    
    myvalue = ExecuteExcel4Macro(strInfoCell)
    

    缺少“&”

    不需要函数

    干杯 尼尔

    【讨论】:

      【解决方案3】:

      类似的应用程序,但没有上述示例中的硬编码路径。此函数从另一个已关闭的工作簿复制值,类似于 =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 Object
      
      Public 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
      

      【讨论】:

        【解决方案4】:
        Data = "'" & GetDirectory & "[" & GetFileName & "]" & Sheet & "'!" & Range(Address).Range("A1").Address(, , xlR1C1)
        
        Address = "$C$3"
        GetDirectory = "C:\Users\my.name\Desktop\"
        GetFileName = "QOS DGL stuff.xlsx"
        Sheet = "ACL"
        

        【讨论】:

          猜你喜欢
          • 2013-06-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-03
          相关资源
          最近更新 更多