• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Microsoft Excel Tips and Tricks - Computergaga

  • Home
  • Excel Online Courses
  • Excel Functions List
  • Free Downloads
  • Blog
  • Tutorials
  • About
Home / VBA / Check if a Value is an Even Number

Check if a Value is an Even Number

I am currently working on an Excel VBA project that requires me to check if the number obtained by Excel is an even number, or if it is an odd number.

I accomplished this using the Mod operator. The Mod (modulus) operator is the VBA equivalent to the MOD function in Excel. It’s purpose is to find the remainder after a number has been divided by a divisor.

The Mod operator is written as below where a and b  represent variables containing values;

a Mod b

If a stored the value 20 and b stored 6 then the result would be 2. And if a stored 20 and b stored 4 then the result would be 0.

To check if a value is an even or an odd number I used the Mod operator to divide the variable holding the number by 2. If the result returned 0 then it must be an even number. And if not then it must be an odd.

The end result was to use an If… Then… Else conditional statement to take action on the answer from the Mod operator. This looked like the below;

If a Mod 2 = 0 then
The number is even, do something
Else
The number is odd, do something else
End If

Reader Interactions

Comments

  1. dinesh Pal says

    23 March 2013 at 12:50 am

    gr8 site for excel vba

    Reply
  2. rice pot says

    6 September 2013 at 9:03 am

    Thanks a lot! This really helped. 🙂

    Reply
  3. David says

    12 October 2022 at 3:36 am

    I’ve been working on an assignment for over two hours and this answer helped me solve the problem in 2 minutes. Thanks so much!

    Reply
    • Alan Murray says

      16 October 2022 at 9:03 pm

      Brilliant! Happy to help, David.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Excel SWITCH Function – Nested IF Function Alternative
  • Sum Formulas Only in Excel
  • How to Calculate Age in Excel
  • Custom Sort in Excel
  • Case Sensitive Lookup with XLOOKUP in Excel
Advanced Excel Success book

Footer

Contact

[email protected]

LinkedIn Twitter YouTube

Useful Links

  • Home
  • Excel Online Courses
  • Tutorials
  • Sitemap
  • Privacy Policy
  • Jobs by Jooble

Course Topics

  • Blog
  • Charts
  • Courses
  • Dashboards
  • Downloads
  • Excel Tips
  • Formulas
  • Mail Merge
  • Office
  • PivotTables
  • Power BI
  • Power Pivot
  • Power Query
  • VBA

Join Our Mailing List

© Copyright 2009 Computergaga · All Rights Reserved ·