testing值是否为长整型(无小数位)

在我的macros,我需要testing如果textbox1和textbox2中的值是整数或长(没有小数位)代码将如何? 我试过这样的东西:

Dim x As Integer Dim y As Integer x = TextBox1.Value y = TextBox2.Value If x = Int(x) Then 'my code Else MsgBox("value is not an integer") 

但它不起作用。 例如,如果我把x = 2.3,它不会显示MsgBox,但它将值四舍五入。请帮忙

既然你已经指定了xtypes是一个Integer ,文本框string到Integer的转换已经发生了。 因此, Int(x)是无操作的。

一个修复就是使用类似的东西

 If IsNumber(TextBox1.Value) Then If Fix(TextBox1.Value) = TextBox1.Value Then 'I am an integeral type. End If End If 

在这里, Fix截断了一个数字types,我依靠VBA来做出正确的比较。 请注意,VBA并没有实现一个简短的循环 And所以你需要使用一个嵌套的If或类似的。

这是另一种方法(伪代码,所以检查语法!)

 Dim l_Temp_D1 as Decimal Dim l_Temp_D2 as Decimal Dim l_Temp_I as Integer l_Temp_I = TextBox1.Value l_Temp_D1 = TextBox1.Value l_Temp_D2 = l_Temp_I if (not IsNumber(TextBox1.Value)) then ' Error!!! End If if (l_Temp_D1 = l_Temp_D2) then ' Integer ... else ' Float ... End If