참조
Sub Demo()
Dim intDay As Integer, firstIter As Integer
Dim startMonth As Date, endMonth As Date
Dim str As String
Dim IsStartMonth As Boolean, IsEndMonth As Boolean
Dim rng As Range, rng1 As Range, rng2 As Range
Dim i As Long
Dim ws As Worksheet
Application.ScreenUpdating = False
firstIter = 1
Set ws = ThisWorkbook.Sheets("Sheet4") 'change Sheet4 to your sheet
IsStartMonth = False
IsEndMonth = False
Do
If Not IsStartMonth Then
'get start date
str = InputBox("Enter Start Date in month-year format " & vbCrLf & "(like Sep 2017 or September 2017)", "Date")
If IsDate(str) Then 'if entery is valid date
startMonth = str
IsStartMonth = True
ElseIf IsEmpty(str) Then 'if nothing is entered
IsStartMonth = True
ElseIf StrPtr(str) = 0 Then 'user clicks close
IsStartMonth = True
Exit Sub
Else 'display input box again
Call MsgBox("Enter a valid date", vbCritical + vbOKOnly, "Date Only")
End If
Else
'get end date
str = InputBox("Enter End Date in month-year format " & vbCrLf & "(like Sep 2017 or September 2017)", "Date")
If IsDate(str) Then 'if entery is valid date
endMonth = DateAdd("d", -1, DateAdd("m", 1, str))
IsEndMonth = True
ElseIf IsEmpty(str) Then 'if nothing is entered
IsEndMonth = True
ElseIf StrPtr(str) = 0 Then 'user clicks close
IsEndMonth = True
Exit Sub
Else 'display input box again
Call MsgBox("Enter a valid date", vbCritical + vbOKOnly, "Date Only")
End If
End If
Loop Until IsStartMonth And IsEndMonth
Set rng = ws.Range("B2")
ws.Range("A2") = "Dates"
Set rng1 = rng.Offset(-1, i)
intDay = intDay + 1
Do
If Format(startMonth + intDay, "ddd") = "Mon" Then 'check whether date is Monday
rng.Offset(-1, i).Value = MonthName(Format(startMonth + intDay, "m"))
rng.Offset(0, i).Value = Format(startMonth + intDay, "d") 'display monday dates
i = i + 1
intDay = intDay + 7
'merge cells in Row 1
If rng1.Value = rng.Offset(-1, i - 1).Value Then
If firstIter <> 1 Then
rng.Offset(-1, i - 1).Value = ""
End If
firstIter = 0
With Range(rng1, rng.Offset(-1, i - 1))
.Merge
.HorizontalAlignment = xlCenter
End With
Else
Set rng1 = rng.Offset(-1, i - 1)
End If
Else
intDay = intDay + 1
End If
Loop Until CDate(startMonth + intDay) > CDate(endMonth) 'loop till start date is less then end date
Application.ScreenUpdating = True
End Sub
참조 이미지를 도움이 될 것입니다 다음.
입력 상자
출력
당신이 요 지금까지 사람들이 도움을 줄 수 있도록 노력했다 코드를 공유 시겠어요! –
죄송합니다. 저와 함께 코드를 갖고 있지 않습니다. 내 사무실 시스템에 있기 때문에 거기에있는 웹에 액세스 할 수 없기 때문입니다. –