【问题标题】:Getting ExecuteExcel4Macro to Recognise Empty Cells让 ExecuteExcel4Macro 识别空单元格
【发布时间】:2017-09-17 05:33:29
【问题描述】:

当我在 VBA 脚本中使用 ExecuteExcel4Macro 函数时:

Public Function GetNamedData(Path, File, Address)
  Dim Data As String
  Data = "'" & Path & File & "'!" & Address
  GetNamedData = ExecuteExcel4Macro(Data)  
End Function

在我的例子中,Address 是一个命名范围,如果关闭的工作簿中的单元格为 ,则此函数返回 0。但是有些单元格包含(故意)0

如何区分 单元格和包含 0 的单元格?

为了进一步解释,我正在使用此函数从已关闭的工作簿中获取数据。事实证明,这可以非常快速地从封闭的工作簿中收集大量数据。

我了解 '0' 和 empty 之间存在差异,但我不确定如何将其用于此函数。

【问题讨论】:

  • @Jeeped 感谢您的帮助。我试图研究并找不到太多有用的东西,因此我现在在这里问。 @ Peh 我正在使用 ExecuteExcel4Macro 从关闭的工作簿中获取数据(不打开它)。我最近才知道这个功能,它使我的数据检索速度更快。如果有更好的方法,请告诉我。
  • @Jeeped 感谢您的意见。我仍然没有建立联系。我理解您所说的关于 ExecuteExcel4Macro 的工作原理...但如果它返回的只是“0”,我怎么知道它实际上是“0”还是空单元格,因为它们都产生相同的结果?跨度>
  • @Peh 好像 Jeeped cmets 消失了?

标签: excel vba macos excel-4.0


【解决方案1】:

试试这个:

GetNamedData = ExecuteExcel4Macro("IF(COUNTA(" & Data & ")," & Data & ",""X"")")

如果关闭的工作簿中的单元格为空,则 GetNamedData = "X"。

【讨论】:

    【解决方案2】:

    下面的函数将值添加一个“”(Null),因此 GetValue2 函数如果为空则返回 null,如果为零则返回零。

    Function GetValue2(strPath As String, strFilename As String, strTabName As String, intColumnNumber As Integer, intRowNumber As Integer) As String
      Dim varCellValue As Variant
      Dim strContents As String
    
      On Error GoTo ErrHandler
    
      varCellValue = ExecuteExcel4Macro("'" & strPath & "[" & strFilename & "]" & strTabName & "'!R" & intRowNumber & "C" & intColumnNumber & " & """"")
      strContents = CStr(varCellValue)
      GoTo NiceExit
    ErrHandler:
      Call MsgBox("strPathFilename : " & strPath & strFilename & Chr(13) & _
               "strTabName      : " & strTabName & Chr(13) & _
               "intColumnNumber : " & intColumnNumber & Chr(13) & _
        "") ' & Error.msg, vbOKOnly)
    NiceExit:
      GetValue2 = strContents
    End Function
    

    【讨论】:

    • varCellValue = ExecuteExcel4Macro("'" & strPath & "[" & strFilename & "]" & strTabName & "'!R" & intRowNumber & "C" & intColumnNumber & " & """" ")
    猜你喜欢
    • 2014-02-08
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多