2016-06-28 1 views
0

저는 startDate로 지정된 시작 날짜와 endDate로 지정된 다른 셀을 가지고 있습니다. 셀을 클릭 할 때마다 캘린더가 튀어 나와 날짜를 입력합니다. 매크로가 endDate를 매월 마지막 날로 자동 변경하기를 원합니다. 따라서 사용자가 들어가서 시작 날짜를 입력하면 매크로는 그 달의 마지막 날인 종료 날짜를 입력합니다. 달력에 대한 코드도 여기에endDate를 월의 마지막 날로 설정하는 VBA를 만들어야합니다.

Function dhLastDayInMonth(Optional endDate As Date = 0) As Date 
    ' Return the last day in the specified month. 
    If endDate = 0 Then 
     ' Did the caller pass in a date? If not, use 
     ' the current date. 
     dtmDate = Date 
    End If 
    dhLastDayInMonth = DateSerial(Year(endDate), _ 
    Month(dtmDate) + 1, 0) 
End Function 

됩니다 :

=DATE(YEAR(startDate),MONTH(startDate)+1,0) 

:

당신의 endDate가 세포에서
Option Explicit 
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
    'check cells for desired format to trigger the calendarfrm.show routine 
    'otherwise exit the sub 
    Dim DateFormats, DF 
    DateFormats = Array("m/d/yy;@", "mmmm d yyyy") 
    For Each DF In DateFormats 
     If DF = Target.NumberFormat Then 
      If CalendarFrm.HelpLabel.Caption <> "" Then 
       CalendarFrm.Height = 191 + CalendarFrm.HelpLabel.Height 
      Else: CalendarFrm.Height = 191 
       CalendarFrm.Show 
      End If 
     End If 
    Next 
End Sub 
+0

매크로는 종료일을 입력합니다 * *? – ThunderFrame

+0

나는 또한 공식이 더 적합하다고 생각합니다. '= EOMONTH (오늘, 0)' – L42

답변

2

,이 공식을 사용할 수 있습니다 여기 코드는 내가 지금 가지고 있습니다 여기서 startDate은 startDate 셀의 명명 된 참조입니다.

0

VBA 함수를 사용하고 싶다면 기본적으로 다음 달의 첫날을 얻고 1 일을 빼면 그 달의 마지막 날을 가져올 수 있습니다. Thunderframe의 대답은 간단하지만 Excel의 기본 기능을 사용하기 때문에 가장 좋다고 생각합니다.

Public Function dhLastDayInMonth(Optional endDate As Date = 0) As Date 
    Dim dt As Date 
    'check if we have a date 
    If endDate = 0 Then 
     ' Did the caller pass in a date? If not, use 
     ' the current date. 
     dt = DateTime.Now 
    Else 
     dt = endDate 
    End If 
    'check for December 
    If DatePart("m", dt) = 12 Then 
     'add one to the year and month 
     dt = DateSerial(DatePart("yyyy", DateAdd("yyyy", 1, dt)), DatePart("m", DateAdd("m", 1, dt)), 1) 
    Else 
     'add 1 to the month 
     dt = DateSerial(DatePart("yyyy", dt), DatePart("m", DateAdd("m", 1, dt)), 1) 
    End If 
    'subtract 1 day from the date to get the last date of the month 
    dt = DateAdd("d", -1, dt) 
    'return the end of the month 
    dhLastDayInMonth = dt 
End Function 
+2

또는 내장 EOMONTH (날짜, 월 수)를 사용할 수 있습니다 ... –

+0

EOMONTH에서 두 번째! – mongoose36