VB and VBA Users Source Code: How to determine if a number is exactly divisible by another number
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
How to determine if a number is exactly divisible by another number
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, April 28, 2005
Hits:
2345
Category:
Visual Basic General
Article:
The simple function below determines if one number is exactly divisible by another. This can be useful when you wish to occassionally check something (eg if the operation has been cancelled) during a large loop (see the "Examples" section in the code header). 'Purpose : Determines if a number goes into another number a whole number of times. 'Inputs : lNumberToCheck The number to check. ' lDivisibleBy The number divide the number to check by. 'Outputs : Returns true if the number to check is exactly divisible by the specified number. 'Author : Andrew Baker 'Date : 08/01/2001 20:24 'Notes : 'Revisions : 'Examples : 'For lThisAttempt = 1 To 1000 ' Sleep 10 ' If IsDivisibleBy(lThisAttempt, 10) Then ' 'Do a task on every 10th iteration ' If bCancelled Then ' Exit For ' End If ' End If 'Next Function IsDivisibleBy(lNumberToCheck As Long, lDivisibleBy As Long) As Boolean If lNumberToCheck Mod lDivisibleBy = 0 And lNumberToCheck <> 0 Then 'lNumberToCheck is exactly divisible by lDivisibleBy IsDivisibleBy= True Else 'lNumberToCheck is not exactly divisible by lDivisibleBy IsDivisibleBy= False End If End Function 'Demonstration code Sub Test() Dim lThisAttempt As Long For lThisAttempt = 1 To 1000 If IsDivisibleBy(lThisAttempt, 10) Then 'Do a task on every 10th iteration Debug.Print "Ran on " & lThisAttempt & " iteration." 'eg: 'If bCancelled Then ' Exit For 'End If End If Next End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder