VBA & Macro Problems

Check Integer Values and Decimal Separators in Excel VBA

John WilsonJohn Wilson Nov 20, 2026 868 views

Question details

Determine whether a number entered in an Excel VBA prompt is an integer while accounting for regional decimal separators.

Check Integer Values and Decimal Separators in Excel VBA
Product
Microsoft Excel VBA
Device & OS
Windows; version not provided
Scenario
A VBA macro accepts numeric input on a system that may use a comma rather than a period as the decimal separator.
Observed behavior
Code that assumes a period as the decimal separator may interpret or reject input incorrectly under a regional configuration that uses a comma.
Before you start

Check which decimal separator Windows uses and whether Excel is set to use system separators. In Excel, open Developer > Visual Basic to edit or test the macro.

Solution 1Recommended

Validate the input and compare it with its integer portion

Use this method when users enter numbers according to the Windows regional settings.

VBA's CDbl conversion uses the system's regional settings. Validate the text first, convert it to a number, and compare it with Int(number). The comparison also works for negative integers.

1
Open the VBA editor

In Excel, select Developer > Visual Basic, then choose Insert > Module.

2
Add the integer-checking macro

Paste this code into the module: Sub CheckInteger() Dim inputText As String Dim number As Double inputText = InputBox("Enter a number:") If Trim$(inputText) = "" Then Exit Sub If Not IsNumeric(inputText) Then MsgBox "Enter a valid number using your Windows regional settings." Exit Sub End If On Error GoTo ConversionError number = CDbl(inputText) MsgBox Int(number) = number Exit Sub ConversionError: MsgBox "The number could not be converted." End Sub

3
Run and test the macro

Return to Excel, press Alt+F8, select CheckInteger, and click Run. Enter an integer or a decimal using the separator configured in Windows; the message displays True for an integer and False otherwise.

Validate the input and compare it with its integer portion
Blank input: The macro exits without a result if the user leaves the prompt blank or cancels it.
Free Microsoft Office alternative

Work with spreadsheets in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar interface and support for common Microsoft Office formats, making it easier to move everyday spreadsheet work between suites. It does not change Excel's VBA or Windows regional settings.

Open and edit spreadsheets in a familiar interfaceCompatible with common Microsoft Excel formats, including XLSXWork with numeric data and spreadsheet formulasUse a free, lightweight alternative for everyday Office tasks
microsoft office alternative - wps office

Frequently Asked Questions

How does the VBA macro decide whether a number is an integer?

After converting the input with CDbl, it compares the number with Int(number). If they are equal, the number has no fractional part.

Will the integer check work for negative numbers?

Yes. Int rounds down to an integer, so Int(-2) equals -2, while Int(-2.5) is -3 and does not equal -2.5.

Where can I change Excel's decimal separator?

Open File > Options > Advanced. Under Editing options, clear Use system separators to set custom decimal and thousands separators, or select it to use the Windows settings.

Does changing Excel's separator change how VBA converts input?

Not necessarily. Excel can use custom separators, but VBA's CDbl conversion uses Windows regional settings. Check both settings if input is rejected or interpreted unexpectedly.