带有错误消息的消息框

时间:2018-04-13 18:32:42

标签: 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
    NumberFormat = "_(£* #,##0.00_);_(£* (#,##0.00);_(£* ""-""??_);_(@_)"

    Else

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

    End If

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

3 个答案:

答案 0 :(得分:1)

使用:

'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

答案 1 :(得分:0)

'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

答案 2 :(得分:0)

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

如果这是在UDF中 - 返回类型Variant,您可以返回CVErr(xlErrValue)CVErr很重要,因为它告诉变体它是一个错误类型,而不仅仅是一个枚举。

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

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