ฟังก์ชันแผ่นงาน VBA | วิธีใช้ WorksheetFunction ใน VBA

ฟังก์ชันแผ่นงาน Excel VBA

ฟังก์ชันแผ่นงานใน VBAใช้เมื่อเราต้องอ้างถึงแผ่นงานเฉพาะโดยปกติเมื่อเราสร้างโมดูลรหัสจะทำงานในแผ่นงานที่ใช้งานอยู่ในปัจจุบันของสมุดงาน แต่ถ้าเราต้องการเรียกใช้รหัสในแผ่นงานเฉพาะเราใช้ฟังก์ชันแผ่นงาน ฟังก์ชันนี้มีการใช้งานและการใช้งานที่หลากหลายใน VBA

สิ่งที่ดีที่สุดเกี่ยวกับ VBA คือการใช้สูตรในแผ่นงานในทำนองเดียวกัน VBA ก็มีฟังก์ชันของตัวเองเช่นกัน ถ้าสิ่งนี้ดีที่สุดมันก็มีสิ่งที่สวยงามเช่นกันนั่นคือ“ เราสามารถใช้ฟังก์ชันแผ่นงานใน VBA ได้เช่นกัน”

ใช่!!! คุณได้ยินถูกต้องเราสามารถเข้าถึงฟังก์ชันแผ่นงานใน VBA ได้เช่นกัน เราสามารถเข้าถึงฟังก์ชั่นเวิร์กชีตบางอย่างในขณะที่เขียนโค้ดและทำให้เป็นส่วนหนึ่งของโค้ดของเรา

วิธีใช้ฟังก์ชั่นแผ่นงานใน VBA

คุณสามารถดาวน์โหลด VBA WorksheetFunction Template ได้ที่นี่ - VBA WorksheetFunction Template

ในแผ่นงานสูตรทั้งหมดจะขึ้นต้นด้วยเครื่องหมายเท่ากับ (=) ในทำนองเดียวกันในการเข้ารหัส VBA เพื่อเข้าถึงสูตรของเวิร์กชีตเราควรใช้คำว่า"WorksheetFunction"

ก่อนที่คุณจะป้อนสูตรเวิร์กชีตใด ๆ คุณต้องระบุชื่ออ็อบเจ็กต์“ WorksheetFunction” จากนั้นใส่จุด (.) จากนั้นคุณจะได้รับรายการฟังก์ชันทั้งหมดที่มีอยู่ในอ็อบเจ็กต์นี้

ในบทความนี้เราจะเน้นเฉพาะการใช้ฟังก์ชันแผ่นงานในการเข้ารหัส VBA ซึ่งจะเพิ่มมูลค่าให้กับความรู้ในการเขียนโค้ดของคุณมากขึ้น

# 1 - ฟังก์ชันแผ่นงาน SUM อย่างง่าย

ตกลงเพื่อเริ่มต้นด้วยฟังก์ชันแผ่นงานให้ใช้ฟังก์ชัน SUM อย่างง่ายใน excel เพื่อเพิ่มตัวเลขจากแผ่นงาน

สมมติว่าคุณมีข้อมูลการขายและค่าใช้จ่ายรายเดือนในแผ่นงานเช่นเดียวกับด้านล่าง

ใน B14 และ C14 เราต้องมาถึงจำนวนทั้งหมดข้างต้น ทำตามขั้นตอนด้านล่างเพื่อเริ่มกระบวนการใช้ฟังก์ชัน“ SUM” ใน Excel VBA

ขั้นตอนที่ 1:สร้างชื่อมาโคร excel ง่ายๆ

รหัส:

 Sub Worksheet_Function_Example1 () End Sub 

ขั้นตอนที่ 2:เนื่องจากเราต้องการผลลัพธ์ในเซลล์ B14 จึงเริ่มโค้ดเป็นRange (“ B14”) ค่า =

รหัส:

 Sub Worksheet_Function_Example1 () ช่วง ("B14") ค่า = End Sub 

ขั้นตอนที่ 3:ใน B14 เราต้องการค่าอันเป็นผลมาจากผลรวมของตัวเลข ดังนั้นในการเข้าถึงฟังก์ชัน SUM จากแผ่นงานให้เริ่มโค้ดเป็น“ WorksheetFunction”

รหัส:

Sub Worksheet_Function_Example1 () ช่วง ("B14") ค่า = WorksheetFunction End Sub

ขั้นตอนที่ 4:เมื่อคุณใส่จุด (.) มันจะเริ่มแสดงฟังก์ชันที่มี ดังนั้นเลือก SUM จากสิ่งนี้

รหัส:

 Sub Worksheet_Function_Example1 () ช่วง ("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 

Step 7: Now run this code manually or using the F5 key to have a total in B14 & C14 cells.

Wow, we got our values. One thing you need to notice here is we don’t have any formula in the worksheet but we just got the result of the “SUM” function in VBA.

#2 – Use VLOOKUP as a Worksheet Function

We will see how to use VLOOKUP in VBA. Assume below is the data you have in your excel sheet.

In E2 cell you had created a drop-down list of all the zones.

Based on the selection you made in the E2 cell we need to fetch the Pin Code for the respective zone. But this time through VBA VLOOKUP, not worksheet VLOOKUP. Follow the below steps to apply VLOOKUP.

Step 1: Create a simple macro name in the Sub Procedure.

Code:

 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.