+ Reply to Thread
Results 1 to 8 of 8

Retrieve last day of the month but hitting error

Hybrid View

alicetsh Retrieve last day of the... 09-04-2014, 08:19 AM
Solus Rankin Re: Retrieve last day of the... 09-04-2014, 08:28 AM
buran Re: Retrieve last day of the... 09-04-2014, 08:34 AM
MickG Re: Retrieve last day of the... 09-04-2014, 08:49 AM
MickG Re: Retrieve last day of the... 09-04-2014, 08:50 AM
alicetsh Re: Retrieve last day of the... 09-04-2014, 07:37 PM
MickG Re: Retrieve last day of the... 09-05-2014, 06:55 AM
buran Re: Retrieve last day of the... 09-05-2014, 10:54 AM
  1. #1
    Registered User
    Join Date
    09-04-2014
    Location
    Australia
    MS-Off Ver
    Mac 2011
    Posts
    2

    Unhappy Retrieve last day of the month but hitting error

    Hi,

    I have month and year in the worksheet where I allowed user to select Month and Year and I wanted to retrieve last day of the Month+Year selected.
    see below codes
    Dim Month As String
    Dim Year As String
    Dim EndDate As Date

    Month = Range("B1").Value 'July
    Year = Range("D1").Value '2014

    EndDate = DateValue((Month + 1) & "/1/" & Year) - 1

    Hitting runtime error '13' : Type mismatch. What am I doing wrong? Please help.

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Retrieve last day of the month but hitting error

    Are the values in B1 and D1 text or numbers?
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Retrieve last day of the month but hitting error

    Hi,
    first of all, please use [code] tags around your code.
    The problem is that Month is a String and you cannot add 1 to a string, you need to convert it to a number type.
    Note that your approach will fail for December, i.e. it's not possible to have date such 13/1/2014.
    You should use something like this
    WorksheetFunction.EOMONTH(DateSerial(CInt(Year),CInt(Month),1),0)
    if you declare Year and Month as Integers, you can skip type conversion.
    If you are pleased with a member's answer then use the Star icon to rate it.

  4. #4
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Retrieve last day of the month but hitting error

    If you month "July" in "B1" is a string then ,Perhaps
    Sub MG04Sep49
    Dim MyMonth As String
    Dim Year As String
    Dim EndDate As Date
    Dim Mth As Integer
        MyMonth = Range("B1").Value 'July '
            Year = Range("D1").Value '2014
                Mth = Month("01-" & MyMonth & "-" & Year)
                    EndDate = DateAdd("d", -1, DateSerial(Year, Mth, "1"))
    End Sub
    Regards Mick

  5. #5
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Retrieve last day of the month but hitting error

    If you month "July" in "B1" is a string then ,Perhaps
    Sub MG04Sep49
    Dim MyMonth As String
    Dim Year As String
    Dim EndDate As Date
    Dim Mth As Integer
        MyMonth = Range("B1").Value 'July '
            Year = Range("D1").Value '2014
                Mth = Month("01-" & MyMonth & "-" & Year)
                    EndDate = DateAdd("d", -1, DateSerial(Year, Mth, "1"))
    End Sub
    Regards Mick

  6. #6
    Registered User
    Join Date
    09-04-2014
    Location
    Australia
    MS-Off Ver
    Mac 2011
    Posts
    2

    Re: Retrieve last day of the month but hitting error

    Hi all,
    Thanks for your reply. Sorry it's my first time posting. Will take note of that.

    I'm hitting error type mismatch for both codes below. Month and Year is a String. I'm using Excell 2011 on Mac. Is that the problem?

    I tried
                     EndDate = WorksheetFunction.EoMonth(DateSerial(CInt(Year), CInt(Month), 1), 0)
    and also

    Sub MG04Sep49
    Dim MyMonth As String
    Dim Year As String
    Dim EndDate As Date
    Dim Mth As Integer
        MyMonth = Range("B1").Value 'July '
            Year = Range("D1").Value '2014
                Mth = Month("01-" & MyMonth & "-" & Year)
                    EndDate = DateAdd("d", -1, DateSerial(Year, Mth, "1"))
    End Sub

  7. #7
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Retrieve last day of the month but hitting error

    I'm not familar with "Mac's " and Excel, although it may be the case.
    I can only suggest you open the VB Code Window and step through the code (Clicking "F8") to see where it fails.
    If you click help in the Vb Code Window I imagine you can see if those Functions like "Month" and "DateAdd" are supported.

  8. #8
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Retrieve last day of the month but hitting error

    Hi,
    it's because you have "July" in B1. My understanding was that you have Month index i.e. 7.
    add this to your module
    Private Function MonthIndex(strMonth As String) As Variant
        Select Case LCase(strMonth)
        Case "january"
            MonthIndex = 1
        Case "february"
            MonthIndex = 2
        Case "march"
            MonthIndex = 3
        Case "april"
            MonthIndex = 4
        Case "may"
            MonthIndex = 5
        Case "june"
            MonthIndex = 6
        Case "july"
            MonthIndex = 7
        Case "august"
            MonthIndex = 8
        Case "september"
            MonthIndex = 9
        Case "october"
            MonthIndex = 10
        Case "november"
            MonthIndex = 11
        Case "december"
            MonthIndex = 12
        Case Else
            MonthIndex = CVErr(xlErrNA)
        End Select
    End Function
    Amend your code as follows:

    Dim Month As Integer
    Dim Year As Integer
    Dim EndDate As Date
    
    
    Month = MonthIndex(Range("B1").Value) '7
    Year = Range("D1").Value '2014
    EndDate = WorksheetFunction.EoMonth(DateSerial(Year, Month, 1), 0)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Error when hitting cancel on Application.InputBox
    By wigtown_deano in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-05-2013, 05:26 AM
  2. Error 1004 - hitting a key in a dropdown list of a userform
    By ggabi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-20-2012, 06:19 AM
  3. retrieve month
    By benj in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-21-2005, 12:16 AM
  4. retrieve month
    By benj in forum Excel General
    Replies: 1
    Last Post: 02-18-2005, 05:29 AM
  5. retrieve month
    By benj in forum Excel General
    Replies: 2
    Last Post: 02-18-2005, 05:23 AM

Tags for this Thread

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