【VBA】Determine if it is even or odd

You can determine if it is even or odd!

Use the following Worksheet function:
 IsEven Function
 IsOdd Function

PR

VBA Code(Determine if it is even)

You can determine if it is even.

Option Explicit

Sub sampleProc()
    
    Dim targetNum As Long
    
    'Set "target Number"
    targetNum = 10

    'Determine if it is even
    If WorksheetFunction.IsEven(targetNum) Then
        MsgBox "『" & targetNum & "』  is even!"
    Else
        MsgBox "『" & targetNum & "』  is not even!"
    End If
    
End Sub

Set “target Number“(Line 8).

Determine if it is even(Line 11~15).


●Result

Result
Result
PR

VBA Code(Determine if it is odd)

You can determine if it is odd.

Option Explicit

Sub sampleProc()
    
    Dim targetNum As Long
    
    'Set "target Number"
    targetNum = 15

    'Determine if it is odd
    If WorksheetFunction.IsOdd(targetNum) Then
        MsgBox "『" & targetNum & "』  is odd!"
    Else
        MsgBox "『" & targetNum & "』  is not odd!"
    End If
    
End Sub

Set “target Number“(Line 8).

Determine if it is odd(Line 11~15).


●Result

Result
Result
PR

FYI

For more information about following, see the following official online manual:

IsEven method” of “WorksheetFunction Object”


ISEVEN Function


“Isodd method” of “WorksheetFunction Object”


ISODD Function

タイトルとURLをコピーしました