How to Check If a Number Is Odd or Even with Excel VBA

Fortunately, many of us use Excel in our business organizations. In any business organization, we use Excel to organize data as needed. Moreover, one thing is that you can check if a number is odd or even with VBA for better operation. However, I have used Office 365 for the purpose of demonstration, and you can use other versions according to your preferences. In this article, I will show you a step-by-step procedure of how to check if a number is odd or even with VBA in Excel. Hence, read through the article to learn more and save time.


Check If a Number Is Odd or Even with Excel VBA: Step-by-Step Procedure to

Often, we need to check if a number is odd or even with VBA for certain business analytics, and the process becomes more interesting with Excel. However, the task is very easy and simple. But you will need an arrangement and a particular VBA code in order to perform the operation properly. Hence, read through the following steps if you want to check the number using Excel VBA properly and easily.


📌 Step 1: Select Dataset

To provide a demonstration, let’s investigate a sample dataset. For instance, the following dataset has two columns titled Number and Type. Here, the Number column contains some numeric values. In contrast, the other column will indicate whether the number is Odd or Even. In addition, I have yet to say that I have been working with the Microsoft Excel 365 version for this post. Nevertheless, you are free to use any other edition that will provide you with the most significant amount of convenience.

vba excel odd or even sample dataset


📌 Step 2: Insert VBA Code to Check Odd or Even

Afterward, a VBA code will help you to check if a number is odd or even in Excel. A VBA code is necessary for this method. Additionally, the process is quite simple and easy to operate. However, you can just copy the code and run it in your Excel worksheet. Hence, go through the following steps in order to complete the task properly.

  • First, hold the Alt + F11 keys in Excel, which opens the Microsoft Visual Basic Applications window.
  • Next, click the Insert button and select Module from the menu to create a module.

Insert VBA Code to Check Odd or Even in Excel

  • Finally, a new window will open and write the following VBA macro in the Module.
Sub CheckEvenOrOddNumber()
    Dim p As Range
    For Each p In Worksheets("VBA").Range("B5:B12")
        If IsNumeric(p.Value) Then
            If p.Value Mod 2 = 0 Then
                p.Offset(0, 1).Value = "Even"
            Else
                p.Offset(0, 1).Value = "Odd"
            End If
        End If
    Next p
End Sub

VBA Code Breakdown

  • First, we create a new procedure Sub in the worksheet using the below statement.
Sub CheckEvenOrOddNumber()
  • Next, we declare variables as
Dim p As Range
  • After that, we activate the VBA sheet and select the Range.
For Each p In Worksheets("VBA").Range("B5:B12")
  • Now, we applied the IF loop to check the cell values whether are even or odd.
   If IsNumeric(p.Value) Then
            If p.Value Mod 2 = 0 Then
                p.Offset(0, 1).Value = "Even"
            Else
                p.Offset(0, 1).Value = "Odd"
  • After that, we started an END If function and activated the cells according to the output.
End If
        End If
    Next p
  • Finally, we end the Sub of the VBA macro as
End Sub
  • Finally, press the F5 key to run the code.

Final Output

Last but not least, you will get your desired output. However, the output will show you whether a given number is odd or even.

vba excel odd or even


💬 Things to Remember

  • First of all, you can choose any number in order to check whether it is even or odd.
  • Then, you have to provide the Range according to your datasheet. Here, insert the range where you want to check the values.
  • Next, you can change the VBA code according to your preference.
  • Afterward, it will show nothing if the value of your cell is not an integer or a text value.
  • At last, you can get the output in a particular cell range or in a MsgBox. For this purpose, you have to modify the code according to your desire.

Download Practice Workbook

You can download the workbook used for the demonstration from the download link below.


Conclusion

These are all the steps you can follow to check if a number is odd or even with VBA in Excel. Overall, in terms of working with time, we need this for various purposes. I have shown multiple methods with their respective examples, but there can be many other iterations depending on numerous situations. Hopefully, you can now easily create the needed adjustments. I sincerely hope you learned something and enjoyed this guide. Please let us know in the comments section below if you have any queries or recommendations.


<< Go Back to | Excel for Math | Excel ODD or EVEN | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Mehedi Hasan
Mehedi Hasan

Mehedi Hasan, a BSc graduate in Naval Architecture & Engineering from Bangladesh University of Engineering and Technology, plays a pivotal role as an Excel & VBA Content Developer at ExcelDemy. Fueled by a profound passion for research and innovation, he actively engages with Excel. In his capacity, Mehedi not only adeptly tackles intricate challenges but also showcases enthusiasm and expertise in navigating tough situations with finesse, underscoring his unwavering dedication to consistently delivering exceptional and high-quality content. He... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo