VBA if语句不匹配错误

时间:2015-03-04 19:06:02

标签: excel vba if-statement

我正在尝试创建一个简单的if语句。当if值不满足else时,这个if语句应移动到else语句,但是excel会给我一个错误13 mismach错误。

这是我的代码:

Sub CALULATE()
Dim postage As String
Dim discountplaceholder As String

discount = Range("d10").Value

If discount = 0.1 Then

MsgBox "hello Bill"
discountplaceholder = 0.1

ElseIf discount = 0.05 Then

MsgBox "hello BOB"
discountplaceholder = 0.05

Else
MsgBox "hello dan"
discountplaceholder = 0

End If
End Sub

第三个单元格值(如果不是0.1或0.05,则为“不适用于折扣”)。有谁知道我怎么能解决这个问题?

提前致谢。

2 个答案:

答案 0 :(得分:0)

您要将discountplaceholder声明为String,但是您要为其指定Double类型的值。将discountplaceholder声明为Double,或为其指定Stringdiscountplaceholder = "0.1"

答案 1 :(得分:0)

Discounterplaceholder声明为Double或使用""为您分配双倍值以将double转换为literal

Dim discountplaceholder As Double

Sub CALULATE()
Dim postage As String
Dim discountplaceholder As String

discount = Range("d10").Value

If discount = "0.1" Then

MsgBox "hello Bill"
discountplaceholder = "0.1"

ElseIf discount = "0.05" Then

MsgBox "hello BOB"
discountplaceholder = "0.05"

Else
MsgBox "hello dan"
discountplaceholder = "0"

End If
End Sub