0

当我在 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”和空之间存在差异,但我不确定如何将其用于此函数。

4

2 回答 2

0

下面的函数将值添加一个“”(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
于 2020-12-10T22:43:20.913 回答
0

试试这个:

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

然后 GetNamedData = "X" 以防关闭的工作簿中的单元格为空。

于 2018-06-25T15:16:19.410 回答