VBA & Macro Problems

Calculate Customer IRR from Multiple Access Fields Using VBA

Ayan MasoodAyan Masood Nov 2, 2026 869 views

Question details

The user needs to calculate the Internal Rate of Return (IRR) using a custom VBA function that can accept an initial amount and multiple separate installment fields as parameters.

How to Calculate Customer IRR from Multiple Fields Using VBA
Product
Microsoft Access / Excel
Device & OS
not provided
Scenario
Passing multiple separate field values (such as an initial investment and subsequent installments) into the built-in IRR function via a custom VBA query or formula.
Observed behavior
The standard VBA IRR function expects an array of Doubles. Passing multiple separate fields requires a ParamArray which inherently creates an array of Variants, causing a type mismatch if not converted before the calculation.
Before you start

Ensure your database or spreadsheet contains the initial investment amount (which must be a negative value) and at least one positive installment value before attempting to calculate the IRR.

Solution 1Recommended

Use a VBA ParamArray Function to Convert Variants to Doubles

Create a User-Defined Function (UDF) that accepts multiple comma-separated fields, converts them into a Double array, and properly passes them to the native IRR function.

A ParamArray allows you to pass an arbitrary number of arguments to a function. However, because ParamArray variables are created as Variants, and the built-in IRR function strictly requires a Double array, you must iterate through the variables and explicitly cast them to Double before calculating.

1
Open the VBA Editor

Press ALT + F11 to open the Visual Basic for Applications (VBA) editor in your Office program, then insert a new Module.

2
Declare the Custom Function

Write the function declaration to accept the ParamArray: `Public Function TheIRR(ParamArray Amount() As Variant) As Double`.

3
Initialize a Double Array

Declare a loop counter and a Double array, then size the array to match the inputs: `Dim i As Long`, `Dim v() As Double`, and `ReDim v(0 To UBound(Amount))`.

4
Convert the Values

Loop through the ParamArray and assign each Variant to your Double array: `For i = 0 To UBound(Amount)` / `v(i) = Amount(i)` / `Next i`.

5
Calculate and Return the IRR

Call the native function using your Double array and assign it to the function name: `TheIRR = IRR(v, 0.1)`. Then close the function with `End Function`.

6
Call the Function in Your Query

In your Access query or Excel cell, call the function by providing your fields separated by commas: `TheIRR([Initial_Amount], [Installment_1], [Installment_2])`.

Use a VBA ParamArray Function to Convert Variants to Doubles
Database Normalization Recommendation: If you are using a database, having repeating numbered columns (like Installment_1, Installment_2) is an unnormalized table structure. Consider redesigning your database to use a related table with one record per installment.
Powerful VBA Support

Calculate IRR with VBA in WPS Spreadsheets

WPS Spreadsheets provides robust support for VBA macros and built-in financial formulas. You can easily create custom UDFs (User-Defined Functions) to calculate Internal Rate of Return (IRR) from multiple spreadsheet cells or database exports without heavy software.

  1. 1. Enable VBA in WPS: Download and install WPS Office. Open WPS Spreadsheets and navigate to the 'Developer' tab to access the built-in VBA environment.
  2. 2. Create a Custom Function: Click 'Visual Basic' to open the editor. Insert a new module and paste your ParamArray function to handle the IRR logic.
  3. 3. Apply Formula to Your Data: Return to your spreadsheet and use your custom function (e.g., =TheIRR(A2, B2, C2)) just like any built-in formula to calculate returns instantly.
Fully compatible with Microsoft Excel (.xlsm) macro-enabled files.Supports standard VBA syntax, including ParamArray and IRR functions.Intuitive VBA Editor for writing and debugging custom financial formulas.Free, lightweight, and fast alternative for advanced data analysis.
microsoft office alternative - wps office

Frequently Asked Questions

Why does the native IRR function return a type mismatch error when I pass multiple fields?

The built-in IRR function in VBA requires a single array of Double-precision numbers as its first argument. If you attempt to pass multiple comma-separated values directly, VBA throws an error because the data types and argument counts do not match the expected structure.

What is a ParamArray in VBA?

ParamArray is a keyword used in a VBA function declaration that allows the function to accept an arbitrary, indefinite number of arguments. It automatically groups these comma-separated arguments into a single array of Variant data types.

How do I handle missing or blank installment fields in the IRR calculation?

Before assigning the ParamArray Variant values to your Double array, you should add a conditional check for Null or empty values (for example, using `If Not IsNull(Amount(i)) Then`). This prevents runtime errors when calculating the IRR on incomplete records.

Should I redesign my database if I have many installment fields?

Yes, having fields strictly named 'Installment_1', 'Installment_2', etc., is considered non-normalized database design. It is highly recommended to use a related secondary table (linked by a foreign key) to store multiple payments flexibly instead of adding hardcoded columns.