百分比类型不匹配错误处理 [英] Percentage Type Mismatch Error Handling

查看:110
本文介绍了百分比类型不匹配错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个宏,它将变量的值格式化为百分比格式。但有时候,有时候不是文本中存在的数字而是文本字符串"N / A"。 (因为没有计算得分)。当发生这种情况时,我得到一个类型不匹配
,因为宏期望一个数字格式化。我想编写一些代码来捕获它并允许文本字符串通过,但如果它是一个数字,我希望它像往常一样继续。我需要的变量是FeeString,QualityString,
NPSString和RefSourceString。任何人都可以帮我写一些像if语句来处理所有这些变量吗?



 NPS = NPSRng.Offset (0,6).Value 
Quality = QualityRng.Offset(0,4).Value
Fee = FeeRng.Offset(0,2).Value

RefSource = sourceRng .Offset(0,7).Value
FormattedRefSource = Format(RefSource," General Number")

FeeString = FormatPercent(Fee,2)
QualityString = FormatPercent(Quality) ,2)
NPSString = FormatPercent(NPS,2)
RefSourceString = FormatPercent(RefSource,2)








MEC

解决方案

Hi MEC,


>>任何人都可以帮我写一些像If语句来处理所有这些变量吗?


在使用FormatPercent之前,我建议你检查值是否为号码。


这是一个简单的代码:

子测试()
Dim r As范围
设置r = ActiveCell
如果是IsNumeric(r.Value)那么
Debug.Print FormatPercent(r.Value,2 )
Else
Debug.Print"它不是有效数字"
结束如果
结束子

最好的问候,


Edward


I have a macro that formats the value of a variable to percentage format. but sometimes instead of a number present in the cell it is sometimes a text string "N/A" (because there is no calculated score). When this happens I get a type mismatch because the macro is expecting a number to format. I'd like to write some code to catch this and allow the text string to pass through but if it is a number I'd like for it to continue as usual. The variables that I need this for are FeeString,QualityString, NPSString and RefSourceString. Can anyone help me write something like an If statement to handle this for all these variables?

NPS = NPSRng.Offset(0, 6).Value
Quality = QualityRng.Offset(0, 4).Value
Fee = FeeRng.Offset(0, 2).Value

RefSource = sourceRng.Offset(0, 7).Value
FormattedRefSource = Format(RefSource, "General Number")

FeeString = FormatPercent(Fee, 2)
QualityString = FormatPercent(Quality, 2)
NPSString = FormatPercent(NPS, 2)
RefSourceString = FormatPercent(RefSource, 2)




MEC

解决方案

Hi MEC,

>> Can anyone help me write something like an If statement to handle this for all these variables?

Before using FormatPercent, I would suggest you check whether the value is number.

Here is a simple code:

Sub test()
Dim r As Range
Set r = ActiveCell
If IsNumeric(r.Value) Then
    Debug.Print FormatPercent(r.Value, 2)
Else
    Debug.Print "It is not valid number"
End If
End Sub

Best Regards,

Edward


这篇关于百分比类型不匹配错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆