function for checking for an odd number

J

Janis

This code works but the division should always give an even number. I'm
trying to error check for an accidental odd number of rows in the
irwo(service group column). The code should continue but give an error
message. There are 2 lines in the code that have division and they should
both be even quotients. Should I put the resume Next on both lines for it to
error check for both lines? The other question is if both numbers are
integers when they divide what would be the function to get it to throw an
error if it is not an even division?

tia,

-----section of code---------

For iRow = LastRow To FirstDataRow Step -1

i = i + 1
If .Cells(iRow, ServiceGroupColumn).Value = .Cells(iRow - 1,
ServiceGroupColumn).Value Then
Else
On Error Resume Next

rowsToAdd = (40 - i) / 2

.Cells(iRow, ServiceGroupColumn).Offset(i /
2).Resize(rowsToAdd).EntireRow.Insert

If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical
End If

i = 1
End If

Next iRow

End With

End Sub
 
G

Gary''s Student

Function testit(n As Long, m As Long) As String
If n Mod m = 0 Then
testit = "even divisor"
Else
testit = "not even divisior"
End If
End Function
 
R

Rick Rothstein \(MVP - VB\)

Here is a function that will return True if the value passed into it is odd
and False if it is even.

Function IsOdd(Value As Long) As Boolean
IsOdd = Value And 1
End Function

So, to use it, you would call it like this...

If IsOdd(SomeValue) Then
' The value in SomeValue is an odd number
Else
' The value in SomeValue is an even number
End If

Obviously, the test is simple enough to use directly within your code,
without encasing it in a Function, if you want...

If SomeValue And 1 Then
' The value in SomeValue is an odd number
Else
' The value in SomeValue is an even number
End If

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top