Excelワークシート関数をVisual Basicで使用する

この文章では、Visual BasicでMicrosoft Excelのワークシート関数を使用する方法について説明しています。一部の関数はVisual Basicには適していませんが、ワークシート関数はWorksheetFunctionオブジェクトを通じて利用可能です。例えば、最小値を求めるMin関数の使用法が示されています。また、セルに関数を挿入する方法や、ローンの支払いを計算するためのPmt関数の例も提供されています。Visual Basic関数とExcel関数では挙動が異なることに注意が必要です。

Excelのワークシート関数をVisual Basicで使用する

Microsoft Excelのワークシート関数は、Visual Basicのステートメント内でも使用することができます。本記事では、Visual Basicで利用可能なワークシート関数のリストについて説明し、具体的な使用例を示します。詳細な情報については、Visual Basicで使用できるワークシート関数のリストをご覧ください。

注意点

一部のワークシート関数はVisual Basicではあまり役に立たないことがあります。たとえば、CONCATENATE関数は必要ありません。なぜなら、Visual Basicでは&演算子を使用して複数のテキスト値を結合できるからです。

Visual Basicからワークシート関数を呼び出す

Visual Basicでは、Excelのワークシート関数はWorksheetFunctionオブジェクトを介して利用可能です。以下のSubプロシージャでは、Minワークシート関数を使用してセルの範囲内の最小値を求めています。

Sub UseFunction() 
    Dim myRange As Range 
    Set myRange = Worksheets("Sheet1").Range("A1:C10") 
    answer = Application.WorksheetFunction.Min(myRange) 
    MsgBox answer 
End Sub

ワークシート関数が引数として範囲参照を必要とする場合、Rangeオブジェクトを指定する必要があります。たとえば、Matchワークシート関数を使用してセル範囲を検索することができます。

Sub FindFirst() 
    myVar = Application.WorksheetFunction.Match(9, Worksheets(1).Range("A1:A10"), 0) 
    MsgBox myVar 
End Sub

注意点

Visual Basicの関数はWorksheetFunctionの修飾子を使用しません。関数はMicrosoft Excelの関数と同じ名前を持つことがありますが、動作が異なる場合があります。たとえば、Application.WorksheetFunction.LogLogは異なる値を返します。

セルにワークシート関数を挿入する

セルにワークシート関数を挿入するには、対応するRangeオブジェクトのFormulaプロパティとして関数を指定します。以下の例では、ランダムな数値を生成するRANDワークシート関数を、アクティブなワークブックのSheet1の範囲A1:B3に割り当てています。

Sub InsertFormula() 
    Worksheets("Sheet1").Range("A1:B3").Formula = "=RAND()" 
End Sub

この例では、Pmtワークシート関数を使用して住宅ローンの月々の支払い額を計算します。InputBoxメソッドを使用することで、型チェックを行います。Staticステートメントを使用すると、3つの変数の値が保持され、次回プログラムを実行する際のデフォルト値として表示されます。

Static loanAmt 
Static loanInt 
Static loanTerm 
loanAmt = Application.InputBox _ 
    (Prompt:="Loan amount (100,000 for example)", _ 
    Default:=loanAmt, Type:=1) 
loanInt = Application.InputBox _ 
    (Prompt:="Annual interest rate (8.75 for example)", _ 
    Default:=loanInt, Type:=1) 
loanTerm = Application.InputBox _ 
    (Prompt:="Term in years (30 for example)", _ 
    Default:=loanTerm, Type:=1) 
payment = Application.WorksheetFunction _ 
    .Pmt(loanInt / 1200, loanTerm * 12, loanAmt) 
MsgBox "Monthly payment is " & Format(payment, "Currency")

参照

他の記事もご覧ください。

サポートとフィードバック

Office VBAやこのドキュメントに関する質問やフィードバックは、Office VBAのサポートとフィードバックを参照してください。サポートを受ける方法やフィードバックの提供方法について案内しています。

————-

Using Excel worksheet functions in Visual Basic

Source link

The article discusses how to use Microsoft Excel worksheet functions within Visual Basic for Applications (VBA). It highlights that most worksheet functions can be accessed through the WorksheetFunction object.

Key points include:

  1. Using Functions in VBA: Some Excel functions, like Concatenate, are not needed in VBA due to the availability of operators (like "&") for text joining.

  2. Calling Functions: The article provides examples of how to call worksheet functions in VBA. For instance, the Min function is used to find the smallest value in a specified range, defined as a Range object, which is then displayed using a message box.

  3. Range References: When using functions that require range references, like the Match function, you must appropriately define a Range object within VBA.

  4. Inserting Formulas: You can insert Excel functions into worksheet cells by setting the Formula property of a Range object, as shown with the RAND function.

  5. Example Calculation: The article gives a practical example of calculating a mortgage payment using the Pmt worksheet function, with input collected via an InputBox for user interaction.

Finally, it invites users to provide feedback or seek help regarding Office VBA.

関連記事

コメント

この記事へのコメントはありません。