+ Reply to Thread
Results 1 to 16 of 16

Issue with month conversion from Sep to 9

Hybrid View

  1. #1
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Issue with month conversion from Sep to 9

    Hi all,

    I have to write a code wherein i have to check if the month in Column E is Sep. I have to then compare this with the current month and then accordingly decide if the data gets copied to a sheet or gets ignored.

    I am not able to compare the months. What i am using is month(date) compared to month(range("E2").value).

    Any suggestions?
    Last edited by arlu1201; 10-04-2011 at 07:28 AM.

  2. #2
    Registered User
    Join Date
    07-21-2011
    Location
    RF, Samara
    MS-Off Ver
    Excel 2003&2007
    Posts
    8

    Re: Issue with month conversion from Sep to 9

    Why doesn't work your method? Show an example of data.

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Issue with month conversion from Sep to 9

    As your range object doesn't have a qualifying workbook or worksheet are you sure that you're looking at the cell you think you are?

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Issue with month conversion from Sep to 9

    If column E is formatted tot string you either:
    - compare strings

       if instr(range("E2").Text,monthname(month(date)))>0
    or
       if instr(range("E2").Text,format(date,"mmmm"))>0
    - or convert the string to a date:
     
       if month(date)=month(cdate(Range("E2")))
    Last edited by snb; 10-04-2011 at 06:46 AM.



  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Issue with month conversion from Sep to 9

    Andrew-R,

    Even if i put
    If Month(CDate(Worksheets(i).Range("E" & j).Value)) = Month(Date)
    It gives me a type mismatch error.

    snb,
    None of the 2 formats are working.

  6. #6
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Issue with month conversion from Sep to 9

    What's i? What's j? Post your file

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Issue with month conversion from Sep to 9

    I have attached the file and the code is in module 1.
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Issue with month conversion from Sep to 9

    You're trying to compare a string value ("SEP") with a date value - that will be your problem.

    If you use Format(Month(Date),"mmm") in your comparison then that will get you past that and on to the next problem with your code

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Issue with month conversion from Sep to 9

    Thank you Andrew-R.

    Just one question - should i format the "sep" in the sheet or should i format today's date which i will be comparing to "sep"?

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Issue with month conversion from Sep to 9

    I actually need the numerical value of the month, so that i can compare it to the other rows as well. I need to ascertain if its equal to this month's number (10), then it should go into the "30 day" tab, if its > than this month, then the "60 day" tab.

  11. #11
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Issue with month conversion from Sep to 9

    You are getting a type mismatch because column E holds strings, not dates.

  12. #12
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Issue with month conversion from Sep to 9

    OK, first of all I just messed up the code, so ignore what I wrote.

    In your code the line doing the comparison should be:

    If Range("E" & j).Value = Format(Date, "mmm") Then

    That should work fine. It doesn't change the format of anything on your sheet, but it will work.

    On the other hand, if your sheet is working with dates then you'd be better off storing them as dates, rather than as strings.

  13. #13
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Issue with month conversion from Sep to 9

    should i format the "sep" in the sheet
    these values in the sheet are text. The only format you can apply to them is cosmetic. Use real dates in these cells, and THEN you can format them to show just the month (using a custom format)

  14. #14
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Issue with month conversion from Sep to 9

    I have actually obtained this sheet from someone else who needs help with VBA. I will try and have them change the values so it will be much easier to code it.

  15. #15
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Issue with month conversion from Sep to 9

    Thank you all.

  16. #16
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Issue with month conversion from Sep to 9

    I have actually obtained this sheet from someone else who needs help with VBA.
    That would be this question, then?

    If you get stuck with a question, you can post into the Cavalry thread in the Water Cooler to call in more expertise. For the OP that will be a lot faster than waiting until you've received answers on your question.
    Last edited by teylyn; 10-04-2011 at 07:45 AM.

+ 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