시작 및 종료 날짜에서 파생 된 날짜 목록이 있습니다. 예 :
01/10/2011
- 에 이르기까지.. - 01/01/2012
VBA에서이 두 날짜 사이의 월 배열을 검색하는 방법은 다음과 같습니다. 출력은 다음과 유사합니다.
Oct-2011
Nov-2011
Dec-2011
Jan-2012
이것에 대한 쉬운 해결책이 있습니까?
워크 시트에 대한 작업없이 VBA에서이 작업을 완전히 수행하려면 다음을 수행하십시오.
날짜를 반복하고 월과 연도를 추출하여 컬렉션에 추가하고 키를 월과 연도의 값으로 설정하여 고유 한 월과 연도로 컬렉션을 만들 수 있습니다.
다른 날짜에 이미 컬렉션에있는 동일한 월과 연도가있는 경우 해당 월과 연도의 키가 이미 설정되어 오류가 발생하므로 컬렉션이 복제하지 않습니다. 오류 처리를 비활성화하면 (On Error Resume Next) 코드가 추가를 건너 뛰므로 컬렉션에서 복제하지 않습니다.
실행 기법 (댓글 포함)
Sub GetUniqueMonths()
Dim uniqueMonths As Collection
Set uniqueMonths = New Collection
Dim dateRange As Range
Set dateRange = Range("A1:A10") 'Change this to your range of dates
On Error Resume Next
Dim currentRange As Range
For Each currentRange In dateRange.Cells
If currentRange.Value <> "" Then
Dim tempDate As Date: tempDate = CDate(currentRange.Text) 'Convert the text to a Date
Dim parsedDateString As String: parsedDateString = Format(tempDate, "MMM-yyyy") 'Format the date into the required format (Oct-2011 etc)
uniqueMonths.Add Item:=parsedDateString, Key:=parsedDateString 'Add the parsed date into the collection
'An error will be thrown if the record already exists as the key has been set to the value (e.g. Oct-2011)
'With On Error Resume next set, it will ignore the error and continue to run without adding the record therefore no duplication of dates
End If
Next currentRange
On Error GoTo 0 'Enable default error trapping
'Loop through the collection and view the unique months and years
Dim uniqueMonth As Variant
For Each uniqueMonth In uniqueMonths
Debug.Print uniqueMonth
Next uniqueMonth
End Sub
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다