+ Reply to Thread
Results 1 to 6 of 6

retrieve month

  1. #1
    Registered User
    Join Date
    02-02-2005
    Posts
    51

    retrieve month

    hi,
    how can i write vba code to retrieve the month from a cell that holds a date

    the problem is that i'm doing a comparison. if the month of the date in row i is p and year is q, where p n q are pre-defined, then i want to copy row i to another sheet. however, running the code below returns an error of Type Mismatch. I assigned P and Q to be integers. so how do i go abt resolving this problem? any kind souls pls help

    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    At what line of code do you get the error. Your month and year part is fine. What is p and q.

    - Mangesh

  3. #3
    Registered User
    Join Date
    02-02-2005
    Posts
    51
    If Month(Cells(i, 4)) = p And Year(Cells(i, 4)) = q Then

    this is the line when the error occurs. apparently it says it is a type mismatch. P n Q are integers.

    P is the month which is obtained from a combo box entry eg Jan, Feb, Mar etc. then i converted it to an integer Jan - 1, Feb - 2 etc.

    Q is the year aso obtained from a combo box entry eg 2003, 2004.

    i understand i cant compare dates and integers but i don't know how i can go about doing this. pls advise

  4. #4
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    Hi Benj,

    Try removing your dim p and q as integers. Also, ensure that p and q are not text. Try printing the values of p and q through a MsgBox statement a,d see if they are correct. Also post your code where you convert the values of p and q to integers.

    - Mangesh

  5. #5
    Registered User
    Join Date
    02-02-2005
    Posts
    51
    below is the code... any kind souls pls help to take a look and give some advice.. thx a million

    Please Login or Register  to view this content.

    i tried putting in msgbox to track the error. apparently, from the IF statement onwards, if the 1st input date is not equal to the date of the combo box, then it is stuck there.
    eg. combo box input : 02/2005

    date at 1st row with data: 11/2004.

    then it will keep going thru IF 11/2004 = 02/2005 and does not proceed from there

  6. #6
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    Try this code instead.

    Please Login or Register  to view this content.
    Note that I have removed the definition of p and r as string. And I am doing a direct compare instead of combining as a date and then comparing.


    - Mangesh

+ 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