简体   繁体   English

在VBA中查找月份或年份的第一天和最后一天的最简单方法

[英]Easiest way for finding first and last day of a Month or year in VBA

Ive two dropdowns for selecting year and month. 我有两个下拉菜单用于选择年份和月份。 In month dropdown there is options JAN,FEB,MAR....DEC and ALL . 在月份下拉列表中,有选项JAN,FEB,MAR....DEC and ALL In Year 2016,2017,2018,2019,2020 2016,2017,2018,2019,2020

I need a function which returns first day of that selection. 我需要一个返回选择第一天的函数。 If 2017 and All selected, it should return 01/01/2017 . 如果选择2017 and All ,则应返回01/01/2017 If its 2017 and JUL , the return value should be 01/07/2017 如果其值为2017 and JUL ,则返回值应为01/07/2017

Also another function just opposite. 另一个功能正好相反。 If user gives 2018 and All , it should return 31/12/2018 . 如果用户给出2018 and All ,则应返回31/12/2018 if user gives 2018 and FEB , it should return 28/02/2018 如果用户给出2018 and FEB ,则应返回28/02/2018

So which is the best solution for this case.. 因此,这是此案的最佳解决方案。

What I tried for first day is as follows 我第一天尝试的内容如下

Function firstDay(year As String, month As String) As Date
    Dim fd As Date
    If LCase(month) = "all" Then month = "JAN"
    fd = DateValue("01 " & month & Space(1) & year)
    firstDay = fd
End Function

But for last day I didn't get how to do that or the solution I tried was not great. 但是对于最后一天,我仍然不知道该怎么做,或者我尝试的解决方案并不理想。 So expecting a better solution in that (Of cource for firstday also if there is better option) 因此,期待有一个更好的解决方案(如果有更好的选择,那么第一天当然也要解决)

This should work for all months, regardless how long they are: 这应该适用于所有月份,无论它们有多长时间:

Function lastDay(year As String, month As String) As Date
    Dim fd As Date
    If LCase(month) = "all" Then month = "DEC"
    fd = DateValue("01 " & month & Space(1) & year) + 32
    fd = fd - Day(fd)
    lastDay = fd
End Function

Im sharing a function I created which serves the purpose. 我分享了我创建的用于目的的功能。 So for those with similar requirement can use 所以对于那些有类似要求的人可以使用

Function lastDay(year As String, month As String) As Date
    Dim ld As Date
    If LCase(month) = "all" Then month = "DEC"
    ld = DateValue("01 " & month & Space(1) & year)
    Dim d As Double
    d = WorksheetFunction.EoMonth(ld, 0)
    ld = CDate(d)
    lastDay = ld
End Function

Here's my version of your function. 这是我的函数版本。

Function endofdates(yr As Integer, _
                    mth As Integer, _
                    Optional firstday As Boolean = True) As Date
    If firstday Then
        endofdates = DateSerial(yr, mth, 0) + 1
    Else
        endofdates = DateSerial(yr, mth + 1, 0)
    End If
End Function

Edit1: Re-read your question and found out that you cannot use above so I've re-written it to suit your needs. 编辑1:重新阅读您的问题,发现您不能在上面使用,因此我已将其重写为适合您的需求。

Function endofdates(yr As Integer, _
                    mth_name As String, _
                    Optional firstday As Boolean = True) As Date
    Dim mth As Integer, idx As Integer
    Const mlist As String = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"
    idx = InStr(1, mlist, mth_name, vbTextCompare)
    mth = Int(idx / 3) + (idx Mod 3)
    If firstday Then
        If mth = 0 Then mth = 1
        endofdates = DateSerial(yr, mth, 0) + 1
    Else
        If mth = 0 Then yr = yr + 1
        endofdates = DateSerial(yr, mth + 1, 0)
    End If
End Function

Edit2: I guess your logic using DateValue is simpler: Edit2:我猜您使用DateValue的逻辑更简单:

Function endofdates(yr As Integer, _
                    mth_name As String, _
                    Optional firstday As Boolean = True) As Date
    Dim dt As Date
    If firstday Then
        If LCase$(mth_name) = "all" Then mth_name = "jan"
        dt = DateValue("1 " & mth_name & " " & yr)
    Else
        If LCase$(mth_name) = "all" Then mth_name = "dec"
        dt = DateValue("1 " & mth_name & " " & yr)
        dt = DateSerial(Year(dt), Month(dt) + 1, 0)
    End If
    endofdates = dt
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM