+ Reply to Thread
Results 1 to 14 of 14

Leap year

  1. #1
    Registered User
    Join Date
    05-26-2008
    Posts
    18

    Leap year

    Hi,

    Ive typed in the following code inVBA to run see which year is a leap year etc. The code is not working form me is this the right function am using??
    Please Login or Register  to view this content.
    Last edited by royUK; 05-28-2008 at 11:21 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    You need to place the True part of the IF test on it's on line, rather than immediately after the THEN

    Please Login or Register  to view this content.
    What exactly is not working, assuming the change does not fix your problem.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    How are you using it? Try the test I have added.

    Also, please use Code Tags in future posts

    Please Login or Register  to view this content.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    05-26-2008
    Posts
    18
    Hi guys thanks for that,

    Ive just started learing how to use VB.
    The task i was given was, in cells A1- A4 enter random years and in cells B1-B4 it would say true or false in the cell.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    The Function works in a worksheet the same as inbuilt functions. Put the year 2008 in A1, in B1 =leapYear(A1). B1 should display true.

  6. #6
    Registered User
    Join Date
    05-26-2008
    Posts
    18
    Hi there thanks for that its working fine.

    Is there a code for the one you gave me in VB??


    thanks again

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Quote Originally Posted by ianma
    Hi there thanks for that its working fine.

    Is there a code for the one you gave me in VB??


    thanks again
    I don't understand, I have given you an example how to use it in VBA earlier.

  8. #8
    Registered User
    Join Date
    05-26-2008
    Posts
    18
    What i mean is,

    In Cell A1- Cell A4 have to put in randon years for example:
    1997

    1998

    2002

    2008

    then i want a VB code to tell me in cell B1- B4 if the year is a leap year or not.
    So if yes it should state True and not it should state False.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You just put the years in column A and the formula in Column B as I explained earlier. Then copy the formula down in Column B

  10. #10
    Registered User
    Join Date
    05-26-2008
    Posts
    18
    ive done it the way have showed me and it works.

    I've been told that ive to use VB and not formattign cells


    Here is the spreadsheet that ive to copy.

    I no my teacher has used everything in vb the only things he has password protected vb so i cant copy his work.
    Attached Files Attached Files

  11. #11
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    If you put this spreadsheet formula in a cell, it will tell you if the date in A2 is in a leap year.
    =IF(DAY(DATE(YEAR(A2),3,1)-1)=29,"Leap Year","Not")

    if just a year is in A2, (i.e. A2 contains 2008 rather than 6/23/2008), that formula would be
    =IF(DAY(DATE(A2,3,1)-1)=29,"Leap Year","Not")
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Quote Originally Posted by ianma
    ive done it the way have showed me and it works.

    I've been told that ive to use VB and not formattign cells


    Here is the spreadsheet that ive to copy.

    I no my teacher has used everything in vb the only things he has password protected vb so i cant copy his work.
    You have the VBA & I have told you how to use it. What part do you not understand?

    I can't open the attachedf workbook, try zipping it first.
    Attached Files Attached Files
    Last edited by royUK; 06-02-2008 at 09:32 AM.

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello ianma,

    After reading your post and looking at the your teacher's code, it appears you need to create a VBA project that will automatically read down column "A" and test if the random years are leap years. Here is a macro to do that.
    Please Login or Register  to view this content.
    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

    Sincerely,
    Leith Ross

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    ianma

    You are not providing enough information. You have the code, your sheet contains the expected results. All you need to do is add the formula that i have already given you in Column C and the result should match Column B.

    If this is not what you want tell us exactly what your teacher requires.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1