MS Excel无法识别MS Access Nz()函数

时间:2018-12-13 16:59:33

标签: excel vba ms-access access-vba

我正在尝试在MS Access中编写一个表达式,该表达式在返回错误后在"New Policy"语句中返回文本字符串Iif()
当我将数据从Access导出到Excel时,它告诉我Nz()无法识别。
有此功能的替代方法吗?我的Access表达式如下:

Nz(IIf([Policy Status]=[Input - WFAC Previous Day]![Policy Status 1],"Remains " & [Policy Status],[Input - WFAC Previous Day]![Policy Status 1] & " to " & [Policy Status]), "New Policy")

3 个答案:

答案 0 :(得分:2)

Nz在Microsoft Access类型库中定义,当您的主机应用程序是Access时,该库会自动引用。

如果在Excel项目中添加对Access类型库的引用,则应该可以使用Nz ...,但是如果未安装Access,则Excel项目将无法工作,这是...怪异而笨拙。

Nz的作用是什么?看起来像某种Coalesce函数,对于自己实现和根据需要进行自定义来说,应该是相当简单的。像这样吗?

Public Function Coalesce(ByVal value As Variant, Optional ByVal value_when_null As Variant = 0) As Variant

    Dim return_value As Variant
    On Error Resume Next 'supress error handling

    If IsEmpty(value) Or IsNull(value) Or (VarType(value) = vbString And value = vbNullString) Then
        return_value = value_when_null
    Else
        return_value = value
    End If

    Err.Clear 'clear any errors that might have occurred
    On Error GoTo 0 'reinstate error handling

    Coalesce = return_value

End Function

答案 1 :(得分:1)

您不需要Nz:

IIf(IsNull([Policy Status]), "New Policy",
IIf([Policy Status] & "" = [Input - WFAC Previous Day]![Policy Status 1],
"Remains " & [Policy Status],
 [Input - WFAC Previous Day]![Policy Status 1] & " to " & [Policy Status]))

答案 2 :(得分:0)

如果您使用的是VBA,则可以添加对MS Access库的引用,例如“ Microsoft Access 14.0对象库。然后Nz函数可以作为Access.Nz(value, "New Policy")使用。您甚至可以创建用户定义的函数以在Excel电子表格中使用:

Public Function Nz(Value As Variant, Optional ValueIfNull As Variant = "New Policy") As Variant
    Nz = Access.Nz(Value, ValueIfNull)
End Function

希望有帮助