VBAワークシート機能| VBAでWorksheetFunctionを使用する方法は?

ExcelVBAワークシート関数

VBAのワークシート関数は、特定のワークシートを参照する必要がある場合に使用されます。通常、モジュールを作成するときに、コードはワークブックの現在アクティブなシートで実行されますが、特定のワークシートでコードを実行する場合は、ワークシート関数を使用します。この関数には、VBAでさまざまな用途と用途があります。

VBAの最も優れている点は、ワークシートで数式を使用する方法と同様に、VBAにも独自の関数があることです。これが最高であれば、「VBAでもワークシート関数を使用できる」という美しい点もあります。

はい!!!あなたはそれを正しく聞いた、私たちはVBAのワークシート関数にもアクセスすることができます。コードの記述中にワークシート関数の一部にアクセスして、コードの一部にすることができます。

VBAでワークシート関数を使用する方法は?

このVBAWorksheetFunctionテンプレートはここからダウンロードできます– VBAWorksheetFunctionテンプレート

ワークシートでは、すべての数式は等号(=)で始まります。同様に、VBAコーディングでは、ワークシートの数式にアクセスするために「WorksheetFunction」という単語を使用する必要があります

ワークシートの数式を入力する前に、「WorksheetFunction」オブジェクト名を指定し、ドット(。)を入力すると、このオブジェクトで使用可能なすべての関数のリストが表示されます。

この記事では、VBAコーディングでワークシート関数を使用する方法に専念します。これにより、コーディングの知識がさらに向上します。

#1-単純なSUMワークシート関数

わかりました。ワークシート関数から始めるには、Excelで単純なSUM関数を適用して、ワークシートから数値を追加します。

以下のようなワークシートに毎月の売上とコストのデータがあるとします。

B14とC14では、上記の数値の合計に到達する必要があります。以下の手順に従って、ExcelVBAで「SUM」機能を適用するプロセスを開始します。

ステップ1:単純なExcelマクロ名を作成します。

コード:

 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。エンドサブ

ステップ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 

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.