VBA function to validate integer?

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
I want to check that the quotient is an integer when I divide a variable by ten. Is there a function for that?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
You can modify this to suit your need, it basically checks if there is any remainder when dividing by 10 using the MOD function.

Code:
Sub Test()
Dim num As Long
Dim Result As String

num = [A1].Value

If num Mod 10 = 0 Then Result = "Integer" Else Result = "Decimal"

[A2].Value = Result
End Sub
 
Upvote 0
If Application.WorksheetFunction.Mod(b, 10) = 0 Then is returning "Object doesn't support property or method. b is dimensioned as an Integer, so I don't see what could be going wrong unless Mod isn't supported...

"Most worksheet functions that are not available as methods of the Application object have an equivalent Microsoft Visual Basic built-in operator or function. For example, the Mod() worksheet function is not available as a method of the Application object because there is an equivalent Mod operator built-in to Visual Basic."

But I already tried Mod(b, 10) without worksheetfunction in front.

Any suggestions?

Edit: Wait, never mind, I see what you did there.

Works great, thanks.
 
Last edited:
Upvote 0
Code:
Dim x As Long
x = 13
MsgBox 10 * Int(x / 10) = x:Rem False

x=120
MsgBox 10 * Int(x / 10) = x:Rem True
 
Upvote 0
Mike: Why the colon after the variable at the end before the Rem statements?

Edit: I don't forsee any problems with this. Do you?

If b Mod 10 = 0 Then
 
Last edited:
Upvote 0
don't forsee any problems with this. Do you?

If b Mod 10 = 0 Then
Try with b = 3000000000 to get error.
It's because Mod works with Long type range of numbers
 
Upvote 0
BTW, the cell function MOD supports full range of Excel numbers, but VBA one is limited by long range of numbers.

In VBA for full range of numbers you can use this UDF to check if the residue of division by ten is equal to zero (similar to Mike's suggestion)
Rich (BB code):

Function IsMultipleTen(Num) As Boolean
  IsMultipleTen = Num / 10 = Fix(Num / 10)
End Function
 
Last edited:
Upvote 0
Mike: Why the colon after the variable at the end before the Rem statements?
[/COLOR]
The colon acts as an indicator that the next bit is to be interpreted as if it were on the next line. Almost the reverse functionality of the continuation underscore.
 
Upvote 0

Forum statistics

Threads
1,224,620
Messages
6,179,927
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top