VBA Testing for integer value

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
516
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a test like x = (b-a)/3

where a & b are row numbers.

How can I test that x is an integer. If x is not an integer, then I have to run another routine.

I'm not sure what vba function I can use to check for whole numbers.

Thanks
 
Noting in case others come across this like I did looking for an answer and it doesn't immediately work as expected

The code works, but not if you declare your variables incorrectly. If you use Integer or Long, x will always evaluate to true so long as it is numeric. If you don't declare, or declare as Single or Double then it will work

Private Sub test1()

'Dim x 'x = 1.4 is not an integer
'Dim x as Integer 'x = 1.4 is an integer
'Dim x as Long 'x = 1.4 is an integer
'Dim x as Single 'x = 1.4 is not an integer
'Dim x As Double 'x = 1.4 is not an integer

x = 1.4

If Int(x) = x Then
MsgBox "x (" & x & ") is an integer"
Else
MsgBox "x (" & x & ") is not an integer"
End If


End Sub
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Forum statistics

Threads
1,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top