p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Excel VBA (http://p2p.wrox.com/forumdisplay.php?f=79)
-   -   Testing for integer (http://p2p.wrox.com/showthread.php?t=10176)

MattLeek March 6th, 2004 07:40 AM

Testing for integer
Hi, im am doing some coursework in my ICT lesson, i am trying to add some Excel VBA which i am learning from reading macros i have made and i also know a bit as some is the same as Access VBA which i learning from a book.

I want to be able to work out whether the current year is a leap year by dividing the current year by four and if it is a whole number then it is. My question is how do i test the number to see if it is a whole number?

Thanks for any help.

Matthew Leek

joefawcett March 6th, 2004 12:05 PM

If your two digit year is contained in iYear then:
Dim dblQuarter as Double
dblQuarter = iYear/4
If dblQuarter = CInt(dblQuarter) Then
'Result is integer
'Result is not integer
End If
Of course this test is not valid as 1900 was not a leap but 2000 was. If you trust Microsoft's date processing you could just see if the 29th February was a valid date:

Function IsLeapYear(iSomeYear As Long) As Boolean
IsLeapYear = Month(DateSerial(iSomeYear, 2, 29)) = 2
End Function

This constructs a date based on 29th February and tests whether the month is still February or has moved to March.



MattLeek March 6th, 2004 12:46 PM

Hi, i hate to be a pain, but i checked in my Access book and couldn't find the DateSerial command. Could anyone tell me how it works?

Matthew Leek

Birger March 6th, 2004 02:52 PM

If the only purpose is to determine whether CURRENT year is a leap year, isn't it sufficient to use this:

Sub LeapYear()

If Year(Now) Mod 4 = 0 Then
    MsgBox ("It's a leap year")
    MsgBox ("Nope")
End If

End Sub

Birger March 6th, 2004 03:09 PM

It might be your book doesn't mention DateSerial, but if you use the help provided by Access you'll find an explanation.

Imar March 6th, 2004 03:27 PM

Nope, it's not

It's a leap year once every four year, unless the year can be divided by 100. And this last rule only applies when the year cannot be divided by 400. So, 2000 was a leap year, 1900 wasn't.



Imar Spaanjaars
Everyone is unique, except for me.

Birger March 6th, 2004 03:35 PM

Oh yeah!
If you're going to use Year(Now), then who's interested in year 2000? Or 1900, for that sake!

If he's using an explicit year, he can disregard year 2000 within an If sentence or use Select Case to disregard that year.

Birger March 6th, 2004 03:52 PM

Could it be that the ICT -word implicates there should be special considerations when answering such question? I can see the it's some sort of interactive education!

joefawcett March 7th, 2004 09:15 AM


quote:Originally posted by MattLeek
 Hi, i hate to be a pain, but i checked in my Access book and couldn't find the DateSerial command. Could anyone tell me how it works?

Matthew Leek
It's not an Access function, it's part of VBA runtime library so can be used across all Microsoft Office apps. Open the help from inside Access VBA IDE and search there.



All times are GMT -4. The time now is 09:44 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.