VBA DatePart 함수 | 날짜의 지정된 부분을 반환하는 방법?

Excel VBA DatePart 함수

VBA의 DatePart 는 인수로 제공된 지정된 날짜의 날짜 부분을 식별하는 데 사용됩니다. 날짜 부분은 일, 월 또는 연도 또는 시간 분과 초가 될 수 있습니다.이 함수의 구문은 자체적으로 매우 명확하고 Datepart (Interval, Date as Argument)입니다.

통사론

DatePart 함수의 구문은 다음과 같습니다.

  • 간격 : 간격 인수로 전달되는 데이터는 문자열 유형입니다. 즉,이 인수에 유효한 값이 포함될 수 있습니다. 간격은 년, 월, 분기, 일, 주,시, 분, 초일 수 있습니다.
  • 날짜 : 평가해야하는 날짜 값입니다.
  • firstdayofweek : 선택적 매개 변수입니다. 이것은 요일을 설명하며 무시할 수도 있습니다. 이 매개 변수를 무시하면 자동으로 일요일을주의 첫 날로 사용합니다. 이를 변경하려면이 매개 변수를 사용할 수 있습니다. 이 인수는 vbUseSystem 0으로 구성 될 수 있습니다.

NLS API 설정 사용

vbSunday (기본값), vbMonday, vbTuesday, vbWednesday, vbThursday vbFriday, vbSaturday.
  • firstweekofyear : top 매개 변수와 마찬가지로이 매개 변수도 선택적 매개 변수입니다. 이것은 한 해의 첫 주를 설명합니다. 이 매개 변수는 무시할 수도 있습니다. 이 매개 변수를 무시하면 1 월 1 일을 해당 연도의 첫 번째 주로 가정합니다. 이를 변경하려면이 매개 변수를 사용할 수 있습니다.

    이 인수는 다음 값으로 구성 될 수 있습니다.

    vbUseSystem, vbFirstJan1, vbFirstFourDays, vbFirstFullWeek.

모든 매개 변수를 제공 한 후 Datepart ()는 전체 날짜, 연도, 월 또는 분기 등과 같은 숫자 값을 반환합니다. 따라서이 함수의 반환 유형은 숫자 값이됩니다.

VBA에서 DatePart 함수를 사용하는 방법은 무엇입니까?

이 VBA DatePart Excel 템플릿은 여기에서 다운로드 할 수 있습니다. – VBA DatePart Excel 템플릿

예 1

첫 번째 예는 전체 날짜와 해당 월의 분기도 표시하는 것입니다.

이 작업을 수행하려면 Visual Basic에서 해당 Goto 개발자 탭에 대한 코드를 작성하고 Visual Basic을 클릭하면 창이 열립니다.

그 창에서 아래와 같이 코드를 작성하십시오.

암호:

 Sub date_Datepart () Dim mydate As Variant mydate = # 12 / 25 / 2019 # MsgBox mydate MsgBox DatePart ( "q", mydate) '표시 분기 End Sub 

이 예에서는 Datepart 함수를 사용하여 날짜와 날짜의 분기 인 날짜의 일부를 표시했습니다. 이것은 어느 분기가 오는 날짜를 표시합니다.

코드를 디버깅하면 "mydate"변수에 임의의 날짜가 할당되어 있기 때문에 코드가 "Msgbox mydate"를 실행하면 처음에 완전한 날짜로 날짜가 표시됩니다.

다음으로 해당 날짜가 해당하는 분기를 표시합니다.

코드를 수동으로 실행하거나 바로 가기 키 F5를 사용하면 확인을 클릭하면 날짜가 표시됩니다. 다음으로 날짜의 분기가 표시됩니다. 아래 스크린 샷에서 볼 수 있습니다.

마찬가지로 분기, 날짜 또는 월 또는 연도 만 표시 할 수 있습니다.

예제 # 2

이 예에서는 런타임에 수동으로 날짜를 입력합니다.

암호:

 Sub date1_datePart() Dim TodayDate As Date ' Declare variables. Dim Msg TodayDate = InputBox("Enter a date:") Msg = "Quarter: " & DatePart("q", TodayDate) MsgBox Msg End Sub 

Here in this example, we are trying to get the date manually at run time. The code “ TodayDate = InputBox(“Enter a date:”)” this line indicates that the date can be entered manually,

After entering the date manually it displays the Quarter of the Date in a message Box. This can be shown in below screenshot.

As the June month is in the 2nd quarter, this displays the 2nd Quarter as shown in the above screenshot.

Example #3

In this example, all the values will be filled up in the cells.

Code:

 Private Sub Workbook_Open() Dim DummyDate As Date DummyDate = ActiveSheet.Cells(2, 2) ActiveSheet.Cells(2, 2).Value = Day(DummyDate) ActiveSheet.Cells(3, 2).Value = Hour(DummyDate) ActiveSheet.Cells(4, 2).Value = Minute(DummyDate) ActiveSheet.Cells(5, 2).Value = Month(DummyDate) ActiveSheet.Cells(6, 2).Value = Weekday(DummyDate) End Sub 

The dates are filled in the cells in the excel sheet, for that the code is written as Active Sheet.cells. By this code the date which is present may be year month or date can be inserted into the given cells.

For example, in the above screenshot,

The day is to be inserted in the cells ( 2, 2) of the excel sheet. Hence the code is written as “ ActiveSheet.Cells(2, 2).Value = Day(DummyDate) “ .

Run the code using the F5 key or manually and the result would be as shown below.

It is by default taking today date and it is displaying as 30 in (2,6) cell.

Likewise for all the other data also it can be filled.

Usage of DatePart Function

  • DatePart function can be used to display the part of the date as the name indicates i.e., if only day or month or year of the date needs to be displayed then this function can be used.
  • This function also separates date, month and a year from a particular date.
  • By using this function the date is not only separated we can also get the quarter, day, hour, minute and a second.

Things to Remember

  • This function can only be used as a VBA Function. In normal excel, this cannot be used.
  • The dates which are given as a value in this function can be given in any format such as mm-dd-yyyy format or DD-MM-YYYY format etc.
  • This function will separate all the values separately such as date, month, year or time also an hour, minute, seconds also.
  • This is organized under Date and Time Functions in VBA of Microsoft Excel.