VBA DateValue | Excel VBA에서 DateValue 함수를 사용하는 방법?

VBA DateValue 함수는 무엇입니까?

DateValue 함수는 날짜 / 시간 함수 범주 아래의 Excel VBA에 내장 된 함수입니다. vba에서 VBA 함수와 워크 시트 함수로 작동합니다. 이 함수는 Date 문자열에서 제공하는 시간 정보를 무시하고 문자열 표현 형식으로 제공된 Date의 일련 번호 또는 값을 반환합니다. Excel에서는 두 가지 방법으로 사용됩니다. 이 함수는 워크 시트 셀에 입력하는 워크 시트 수식으로 사용됩니다. Microsoft Excel과 관련된 Visual Basic Editor를 통해 입력하는 VBA 응용 프로그램의 매크로 코드로 사용됩니다.

이 기사에서는 VBA DATEVALUE의 예와이를 명확한 설명과 함께 사용하는 방법에 대해 알아 봅니다.

VBA Datevalue 함수 설명

VBA에서 DATEVALUE는 다음 구문을 사용합니다.

이 함수는 단일 인수 또는 매개 변수 만 사용합니다.

  • 날짜 : 문자열 형식으로 표시되는 날짜입니다.
  • 반환 : 이 함수는 VBA 함수로 사용될 때 날짜 값을 반환합니다. 워크 시트 함수로 사용될 때 날짜 값을 반환합니다.

VBA DateValue 함수는 유효한 Excel 형식으로 언급 된 텍스트 형식으로 표시된 데이터를 해석 할 수 있습니다. 문자열에 요일의 텍스트 표현이 포함 된 경우 날짜 값을 반환 할 수 없습니다.

VBA DateValue 함수의 이점은 문자열에서 날짜 값을 추출하고 시간이있는 날짜를 유일한 날짜로 변환하는 것입니다. 날짜가 시간과 함께 주어지면이 함수는 시간 값을 피하는 날짜 값만 사용할 수 있습니다.

Excel VBA DATEVALUE를 사용하는 방법?

Excel에서 DateValue 함수를 사용하려면 먼저 VBA 편집기를 열어야합니다.

VBA 프로그램의 행을 추가하려면 명령 단추를 Excel 워크 시트에 배치해야합니다. 프로그램 라인을 구현하려면 사용자가 Excel 시트에서 명령 버튼을 클릭해야합니다. 프로그램에서 유효한 출력을 얻으려면 인수를 통해 유효한 입력이 제공됩니다. 예를 들어 다음 코드는 VBA의 텍스트에서 날짜 값을 추출하는 DateValue 함수를 실행하는 매크로를 만드는 데 도움이됩니다.

VBA 프로그램 : 

 Sub Datebutton () Dim myDate As Date myDate = DateValue ( "1991 년 8 월 15 일") MsgBox Date (myDate) End Sub 

이 코드는 주어진 입력에서 날짜를 15로 만듭니다.

Excel VBA DATEVALUE의 예

다음은 Excel VBA의 DateValue 예입니다.

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

예제 # 1 – 날짜에서 일, 월, 연도 얻기

VBA에서 프로그램을 만들고 실행하기 위해 몇 가지 단계를 따릅니다. 여기에는

1 단계 : 개발자 탭으로 이동하여 Excel 시트의 셀에 커서를 놓고 '삽입'옵션을 클릭하고 그림과 같이 ActiveX 컨트롤에서 '명령 버튼'을 선택합니다.

원하는 위치로 Button을 드래그하고 속성 창에서 캡션을 Datebutton으로 지정합니다.

2 단계 : 버튼을 두 번 클릭하면 VBA 프로젝트로 리디렉션되고 Private Sub 명령 버튼과 end sub 사이에 코드가 작성됩니다.

날짜, 월, 연도를 얻으려면 코드를 아래와 같이 개발해야합니다.

암호:

 Private Sub Datebutton1_Click () Dim Exampledate As Date Exampledate = DateValue ( "April 19,2019") MsgBox Date MsgBox Year (Exampledate) MsgBox Month (Exampledate) End Sub 

이 코드에서 Datebutton1_Click ()은 이름이고 exampledate는 Date 데이터 유형과 Msgbox를 사용하여 출력을 표시하는 변수입니다.

3 단계 : 코드를 개발하는 동안 vba 유형 불일치 오류가 발생하고이를 처리해야합니다.

4 단계 : 이 단계에서는 실행 옵션을 클릭하여 프로그램을 실행합니다.

또는 디버그 메뉴에서 Step Into 옵션을 선택하여 단계별로 프로그램을 확인하거나 디버그 할 수 있습니다. 코드에 오류가 없으면 출력이 표시됩니다.

5 단계 : 프로그램이 실행되면 먼저 텍스트 입력에 지정된 날짜와 함께 메시지 상자가 표시됩니다. 그런 다음 확인을 클릭하여 연도 값을 다시 확인합니다. 메시지 상자에서 확인을 클릭하여 월 값을 확인합니다.

참고 : 정확한 결과를 얻으려면이 단계를 명확하게 따라야합니다.

Example #2 – Using the DatePart to Get the Different Parts of Date

Step 1: Go to Excel Developer Tab, place cursor on a cell in the Excel sheet and click on ‘Insert’ option and choose ‘Command Button’ under ActiveX Control as shown in the figure.

Step 2: Drag the button and give the caption as DatePart under properties.

Double click on this button, it directs to the Visual Basic Editor sheet and displays as follows.

Step 3: Develop the code using the DatePart with DateValue as shown in the figure.

Code:

 Private Sub Datepart1_Click() Dim partdate As Variant partdate = DateValue("8/15/1991") MsgBox partdate MsgBox Datepart("yyyy", partdate) MsgBox Datepart("dd", partdate) MsgBox Datepart("mm", partdate) MsgBox Datepart("q", partdate) End Sub 

In this program, DatePart1 is the macro name and partDate is argument name with data type ‘variant’. For displaying year, date, month, and a quarter, the format is applied as “yyyy”, “d”, “m”, and “q”. If we do any mistake in the format it displays the following error.

Step 4: After successful debugging of program, run the program by clicking on the run button use excel shortcut key F5.

The code first displays the full date then after clicking every OK from the msgbox, it shows the year value after that Date value, Month Value, Quater Value respectively.

Things to Remember About the VBA DATEVALUE

The following things must be remembered while using the DateValue function in Excel VBA

  • Run time error 13 with message Type Mismatch is displayed when the date provided to the DateValue function is not able to convert into a valid date. We need date is proper text format
  • When we try to get the only date from the string argument with code ‘msgbox date (argument name)’, it displays the type mismatch error.
  • We can see the output of the DateValue function without opening the VBA editor. It is done by clicking the command button and select the macro created for the respective program
  • When DatePart is used to get the values, the appropriate format should be followed. Otherwise, it leads ‘run time error 5’ with message invalid procedure call or argument.