Skip to content

Use VBA to extract the currency from a number format

When an export holds the currency only in a custom number format, the CELL function cannot read it. A short VBA function can, and a lookup does the rest.

Article · 3 minute read · 27 September 2021

Data exported from accounting, sales or product systems sometimes arrives with custom Excel number formats. Usually you can change them to whatever you prefer. The exception is when the format holds information you need.

When the currency sits only in the number format

In the example below, the export gives each product value as a number. The currency appears only in the custom number format, with a different format for each currency: Product A is in CAD and Product B is in GBP.

The aim is to extract the currency code (CAD, GBP and so on) into its own column, CUR. That column then drives the FX lookup, to give a final value in GBP or whichever currency you report in.

Exported product values in Excel, with each currency shown only through a custom number format
Figure 1: The exported data, with the currency held in the number format
The custom number format used for CAD values in Excel
Figure 2: The custom number format used for CAD
The custom number format used for USD values in Excel
Figure 3: The custom number format used for USD

Why the CELL function does not work here

The option most often suggested for reading a cell’s number format is the CELL function. It has a clear limit with custom number formats that use a different currency in each cell, whether they were set by hand or by the system that exported the data.

The CELL function applied to a cell with a custom currency format, returning a result that does not identify the currency
Figure 4: The CELL function on a custom currency format

Applied to our custom format, CELL returns “,2”. That tells us nothing about which currency the cell uses.

For more on custom number formats, see How to speed up your models, part 3.

A VBA function to extract the currency

A user defined function (UDF) in VBA can extract the currency in three steps:

  1. Read the full custom number format as text with the cell’s NumberFormat property. It returns something like “CAD” #,##0.00;”CAD” -#,##0.00.
  2. Trim the text with MID so only the currency code is left.
  3. Return the currency code to the worksheet as a string.

If you have not written a UDF before, Microsoft’s support pages include an introduction to creating custom functions in Excel. For our training, see our financial modelling courses.

You could do the FX lookup in VBA too. For transparency we recommend keeping the VBA to a minimum and leaving most of the logic in Excel, where a reviewer can see it.

Function ExtractCurrency(FormattedCell As Range) As String
'This function extracts the number format from Currency formatted cells (CAD/GBP/USD, etc)
'Useful in the specific situation where DIFFERENT formatting codes have been used in a range
Dim strNumberformat As String
Dim strNumberformatClean As String
    'pick up the numberformat from the Cell property
    'should read something like ""CAD" #,##0.00;"CAD" -#,##0.00"
    strNumberformat = FormattedCell.numberformat
    'Clean up the string
    strNumberformatClean = Mid(strNumberformat, 2, 3)
    'Alternative (step-by-step) approach for debugging
    'strNumberformatClean = Left(strNumberformat, 4)
    'strNumberformatClean = Right(strNumberformatClean, 3)
    'Return the extracted currency code to the active cell
    ExtractCurrency = strNumberformatClean
End Function
The ExtractCurrency function used in the worksheet to return the currency code
Figure 5: The ExtractCurrency function in use

Completing the conversion with XLOOKUP

Once the currency code is in its own column, the rest is straightforward. The simplest and most modern way is an XLOOKUP against an FX table. INDEX and MATCH work just as well if you prefer them.

Be careful with LOOKUP: it needs the data sorted. If someone later adds a currency to the table out of order, the lookup returns the wrong rate without any warning. For more on LOOKUP against VLOOKUP and HLOOKUP, see Use LOOKUP to replace VLOOKUP and HLOOKUP.

The final step of the example: the currency conversion completed with a lookup in Excel
Figure 6: Completing the conversion with a lookup

Learn financial modelling in a room

Hands-on courses in person and online, taught to more than 20,000 people on one modelling standard.

Best Practice Project Finance Modelling

Build and sensitise robust cashflow models from scratch, faster in Excel. Hands-on, practitioner-led, in person or online. See dates and book your place.

View full course details: Best Practice Project Finance Modelling

Further Financial Modelling for Advanced Modellers

Further Financial Modelling for Advanced Modellers: shareholder loans, mezzanine debt, regearing, FX and financial-year tax.

View full course details: Further Financial Modelling for Advanced Modellers

Advanced Project Finance Modelling

Debt sizing, covenant monitoring, DSRAs and integrated three-way statements. Hands-on, practitioner-led training, in person or online. See dates and book.

View full course details: Advanced Project Finance Modelling

Browse all training courses

More on this topic

Tutorials

Use LOOKUP to replace VLOOKUP and HLOOKUP in Excel

How the LOOKUP function can replace VLOOKUP and HLOOKUP in a financial model, with a step-by-step guide to using it on the schedules you already have.

Excel shortcuts for financial modelling

The Excel shortcuts we use most in financial modelling, covering selection, formatting, filling formulas down and other everyday ways to save time.

Services

Financial Modelling Support

Experienced modellers who work alongside your team when you need them, to update a model, add a refinancing case or check work before it goes out.

Financial Model Development

Transaction and operational models built to one modelling standard, documented for lenders and investors and handed over with the logic explained.

Learn to build these models yourself

We run public and in-house courses across the full financial modelling curriculum.