VBA 워크 시트 기능 | VBA에서 WorksheetFunction을 사용하는 방법?

Excel VBA 워크 시트 함수

VBA의 워크 시트 함수 는 특정 워크 시트를 참조해야 할 때 사용됩니다. 일반적으로 모듈을 만들 때 코드는 통합 문서의 현재 활성 시트에서 실행되지만 특정 워크 시트에서 코드를 실행하려면 워크 시트 함수를 사용합니다. 이 기능은 VBA에서 다양한 용도와 응용이 있습니다.

VBA의 가장 좋은 점은 워크 시트에서 공식을 사용하는 것과 비슷하게 VBA에도 자체 기능이 있다는 것입니다. 이것이 최고라면 "VBA에서도 워크 시트 함수를 사용할 수 있습니다"라는 아름다운 것도 있습니다.

예!!! 잘 들었을 때 VBA의 워크 시트 함수에도 액세스 할 수 있습니다. 코드를 작성하는 동안 일부 워크 시트 함수에 액세스하여 코드의 일부로 만들 수 있습니다.

VBA에서 워크 시트 함수를 사용하는 방법은 무엇입니까?

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

워크 시트에서 모든 수식은 등호 (=) 기호로 시작합니다. 워크 시트 수식에 액세스하려면 "WorksheetFunction" 이라는 단어를 사용해야합니다 .

워크 시트 수식을 입력하기 전에 "WorksheetFunction"개체 이름을 언급 한 다음 점 (.)을 입력하면이 개체 아래에 사용 가능한 모든 함수 목록이 표시됩니다.

이 기사에서는 코딩 지식에 더 많은 가치를 추가 할 VBA 코딩에서 워크 시트 기능을 사용하는 방법에 독점적으로 집중할 것입니다.

# 1 – 간단한 SUM 워크 시트 함수

좋아, 워크 시트 함수로 시작하려면 Excel에서 간단한 SUM 함수를 적용하여 워크 시트에서 숫자를 추가합니다.

아래 그림과 같은 워크 시트에 월별 매출 및 비용 데이터가 있다고 가정합니다.

B14와 C14에서 우리는 위의 총합에 도달해야합니다. Excel VBA에서 "SUM"기능을 적용하는 프로세스를 시작하려면 아래 단계를 따르십시오.

1 단계 : 간단한 엑셀 매크로 이름을 만듭니다.

암호:

 Sub Worksheet_Function_Example1 () End Sub 

2 단계 : B14 셀에 결과가 필요하므로 코드를 Range (“B14”) 로 시작합니다 .Value =

암호:

 Sub Worksheet_Function_Example1 () Range ( "B14"). Value = End Sub 

3 단계 : B14에서는 숫자 합계의 결과로 값이 필요합니다. 따라서 워크 시트에서 SUM 함수에 액세스하려면 코드를 "WorksheetFunction" 으로 시작하십시오 .

암호:

Sub Worksheet_Function_Example1 () Range ( "B14"). Value = WorksheetFunction. End Sub

4 단계 : 점 (.)을 입력하면 사용 가능한 기능이 표시되기 시작합니다. 따라서 여기에서 SUM을 선택하십시오.

암호:

 Sub Worksheet_Function_Example1 () Range ( "B14"). Value = WorksheetFunction.Sum End Sub 

5 단계 : 이제 위의 숫자, 즉 범위 (“B2 : B13”)에 대한 참조를 제공합니다 .

암호:

 Sub Worksheet_Function_Example1 () Range ( "B14"). Value = WorksheetFunction.Sum (Range ( "B2 : B13")) End Sub 

6 단계 : 유사하게 다음 열에 대해 셀 참조를 변경하여 유사한 코드를 적용합니다.

암호:

 Sub Worksheet_Function_Example1 () Range ( "B14"). Value = WorksheetFunction.Sum (Range ( "B2 : B13")) Range ( "C14"). Value = WorksheetFunction.Sum (Range ( "C2 : C13")) End Sub 

7 단계 : 이제이 코드를 수동으로 실행하거나 F5 키를 사용하여 B14 및 C14 셀의 합계를 얻습니다.

와우, 우리는 가치를 얻었습니다. 여기서 주목해야 할 한 가지는 워크 시트에 수식이 없지만 VBA에서 "SUM"함수의 결과를 얻었습니다.

# 2 – VLOOKUP을 워크 시트 함수로 사용

VBA에서 VLOOKUP을 사용하는 방법을 살펴 보겠습니다. 아래는 Excel 시트에있는 데이터라고 가정합니다.

E2 셀에서 모든 영역의 드롭 다운 목록을 만들었습니다.

E2 셀에서 선택한 항목에 따라 각 영역에 대한 핀 코드를 가져와야합니다. 그러나 이번에는 VLOOKUP 워크 시트가 아닌 VBA VLOOKUP을 통해 수행합니다. VLOOKUP을 적용하려면 아래 단계를 따르십시오.

1 단계 : 하위 프로 시저에서 간단한 매크로 이름을 만듭니다.

암호:

 Sub Worksheet_Function_Example2 () End Sub 

Step 2: We need the result in the F2 cell. So start the code as Range (“F2”).Value =

Code:

 Sub Worksheet_Function_Example2() Range ("F2").Value = End Sub 

Step 3: To access worksheet function VLOOKUP starts the code as “WorksheetFunction.VLOOKUP”.

Code:

 Sub Worksheet_Function_Example2() Range ("F2").Value = WorksheetFunction.Vlookup( End Sub 

Step 4: One of the problems here is syntax will not give you any sort of guidance to work with VLOOKUP. You need to be absolutely sure about the syntax you are working on.

The first syntax of VLOOKUP is “Lookup Value”. In this case, our lookup value is E2 cell value, so write the code as Range (“E2”).Value

Code:

 Sub Worksheet_Function_Example2() Range ("F2").Value = WorksheetFunction.Vlookup(Range ("E2").Value, End Sub 

Step 5: Now the second argument is our table array, in this case, our table array range is from A2 to B6. So the code will be Range (“A2:B6”)

Code:

 Sub Worksheet_Function_Example2() Range ("F2").Value = WorksheetFunction.Vlookup(Range ("E2").Value,Range ("A2:B6"), End Sub 

Step 6: The Third argument will be from which column we need the data from the table array. Here we need the data from the 2nd column, so the argument will be 2.

Code:

 Sub Worksheet_Function_Example2() Range ("F2").Value = WorksheetFunction.Vlookup(Range ("E2").Value,Range ("A2:B6"),2, End Sub 

Step 7: The final argument is range lookup, we need an exact match so the argument is zero (0).

Code:

 Sub Worksheet_Function_Example2() Range("F2").Value = WorksheetFunction.VLookup(Range("E2").Value, Range("A2:B6"), 2, 0) End Sub 

So, we are done with the coding part. Now go to the worksheet and select any of the range.

Now go to your coding module and run the macro Using F5 key or manually to get the pin code of the selected zone.

We cannot go back and run the macro every time, so let’s assign a macro to shapes. Insert one of the shapes in a worksheet.

Add a text value to inserted shape.

Now right click and assign the macro name to this shape.

Click on ok after selecting the macro name.

Now, this shape holds the code of our VLOOKUP formula. So whenever you change the zone name click on the button, it will update the values.

Things to Remember

  • To access worksheet functions we need to write the word “WorksheetFunction” or “Application.WorksheetFunction”
  • We don’t have access to all the functions only a few.
  • We don’t see the actual syntax of worksheet functions, so we need to be absolutely sure of the function we are using.