VB Excel 2010运行时错误' 13':类型不匹配

时间:2014-05-30 11:53:40

标签: excel-vba runtime-error vba excel

我要做的就是让一个单元格显示两个其他单元格之间的百分比增加/减少。当有数字值但按下重置后,代码工作正常。按钮(将单元格值设置为" - "),即使我已经使用了" - "它也不会工作。场景到我的代码中:

Private Sub PerChange(ByVal burn1 As Range, burn2 As Range, change As Range)

If burn1.Value And burn2.Value = "-" Then
    change.Value = "-"
Else
    change.Value = (burn1.Value / burn2.Value) - 1
End If

End Sub

即使两者的值都是" - "

,也是出现错误的粗线。

我还尝试将粗体换成:

**If IsNumeric(burn1.value and burn2.value) = False**

并且它仍然在此行上的TYPE不匹配时停止,即使IsNumeric的整个点是检查非数字类型!!

请告知我正在撕扯我的头发......

1 个答案:

答案 0 :(得分:0)

这将有效:

Private Sub PerChange(ByVal burn1 As Range, burn2 As Range, change As Range)
    If burn1.Text = "-" And burn2.Text = "-" Then
        change.Value = "-"
    Else
        change.Value = (burn1.Value / burn2.Value) - 1
    End If
End Sub

测试:

Sub MAIN()
    Call PerChange(Range("A1"), Range("A2"), Range("B1"))
End Sub