让ExecuteExcel4Macro识别空单元格

时间:2017-04-20 14:10:04

标签: excel vba excel-vba excel-vba-mac

当我在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'之间存在差异。并且为空,但我不确定如何将其用于此功能。

2 个答案:

答案 0 :(得分:0)

尝试这个:

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

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

答案 1 :(得分:0)

下面的函数在值上添加一个“”(空),因此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