Vba 带有错误消息的消息框

Vba 带有错误消息的消息框,vba,excel,Vba,Excel,我目前有以下消息框和代码: 'Message box to ensure that the accounting format equals the loan withdrawl country If txtloanwithcountry = "England" Or txtloanwithcountry = "Wales" Or txtloanwithcountry = "Scotland" Or txtloanwithcountry = "Norther-Ireland" Then

我目前有以下消息框和代码:

'Message box to ensure that the accounting format equals the loan withdrawl country
    If txtloanwithcountry = "England" Or txtloanwithcountry = "Wales" Or txtloanwithcountry = "Scotland" Or txtloanwithcountry = "Norther-Ireland" Then
    NumberFormat = "_(£* #,##0.00_);_(£* (#,##0.00);_(£* ""-""??_);_(@_)"

    Else

    MsgBox "The currency used in the loan withdrawl country must be equal to number format"

    End If
但是,我想将其显示为错误,因此它不会显示在工作表中。有人知道这个密码吗

使用:

'Message box to ensure that the accounting format equals the loan withdrawl country
If txtloanwithcountry = "England" Or txtloanwithcountry = "Wales" Or txtloanwithcountry = "Scotland" Or txtloanwithcountry = "Norther-Ireland" Then
NumberFormat = "_(£* #,##0.00_);_(£* (#,##0.00);_(£* ""-""??_);_(@_)"

Else

MsgBox "The currency used in the loan withdrawl country must be equal to number format", vbCritical

End If

根据设置
NumberFormat
(以及相关值)的方式/位置,这是
Variant
非常有用的情况之一

如果这是在UDF中,则使用返回类型
Variant
,您可以返回
CVErr(xlErrValue)
CVErr
非常重要,因为它告诉变量它是一种错误类型,而不仅仅是一种枚举

如果直接输入到单元格中,可以使用相同的变量值


在这两种情况下,它都将显示为
#Value在工作表中。

是否可以确保我在用户表单中输入的值不会显示在工作表中?@Gjovis您需要在原始问题中添加更多内容-您从未提到过用户表单,我们不知道您是如何获得这些值的——很可能您想阻止用户从一开始就输入这些值。
'Assuming the txtloanwithcountry string is in cells(2,2) on the activesheet
'Assuming the cell to be formatted is cells(2,4) on the activesheet
'Edit: I am changing the cell's font color to white if there is an error in
'      the Country to hide the Error Message so it does not show up

Sub FormatCurrency()

Dim ws as Worksheet
Dim rng as Range
Dim txtloandwithcountry as String

Set ws = ActiveSheet
Set rng = ws.Cells(2,2)

With rng

    .offset(0, 2).Value = ""
    .offset(0, 2).NumberFormat = "General"
    .offset(0, 2).Font.Color = vbBlack

    txtloanwithcountry = .Value

    Select Case txtloanwithcountry

        Case "England", "Wales", "Scotland", "Northern-Ireland"

            .Offset(0, 2).NumberFormat = "_(£* #,##0.00_);_(£* (#,##0.00);_(£* ""-""??_);_(@_)"

        Case Else

            .Offset(0, 2).Value = "ERROR"
            .Offset(0, 2).Font.Color = vbWhite

    End Select

End With

End Sub