 # Formula Forensics No. 041 – Convert a Roman Numeral to a Number

Share

Last week in the Chandoo.org Forums a user asked a question

How do I convert a Roman Numeral to a Number eg: MMMCCCLVII to 3357

User Xlstime presented the solution of:

=MATCH(A2,INDEX(ROMAN(ROW(INDIRECT(“1:3999”))),0),0)

Today we are going to look at how and why that simple formula works

## Solution

Excel has a Roman function wherein =Roman(3357, 0) will return MMMCCCLVII

I knew there was no such reverse Roman function prior to Excel 2013 and so my initial thought was to look at a VBA Solution.

See notes on the Arabic Excel function at the end of the post.

However Xlstime presented =MATCH(A2,INDEX(ROMAN(ROW(INDIRECT(“1:3999”))),0),0)

How does this work?

lets start by pulling it apart from the inside out

=MATCH(A2,INDEX(ROMAN(ROW(INDIRECT(“1:3999”))),0),0)

The Indirect Function simply takes its inputs and converts them to a Range, in this case 1:3999.

We will study why 3999 later

=MATCH(A2,INDEX(ROMAN(ROW(INDIRECT(“1:3999”))),0),0)

The next function working out is Row()

Excel will convert the function ROW(INDIRECT(“1:3999”)) to an array of Row Numbers

={1;2;3;4;5;6;7;8;9;10;11; …. 3995;3996;3997;3998;3999}

You can see this if you goto cell D7 in the Sample File, press F2 and then F9

I limited the numbers to 300 as Excel cannot display more than 8,192 digits

Stepping out one more function:

=MATCH(A2,INDEX(ROMAN(ROW(INDIRECT(“1:3999”))),0),0)

The Roman() function converts its inputs into Roman Numbers

eg: Roman(58) will return LVIII

But as we are feeding it an array of numbers from 1 to 3999 Excel handles all these and converts them to an Array of Roman Numbers

Goto  D9 in the sample file =ROMAN(ROW(INDIRECT(“1:300”))) press F2 and then F9

Excel returns an array of roman numbers

={“I”;”II”;”III”;”IV”;”V”;”VI”;”VII”;”VIII”; … “CCXCVI”;”CCXCVII”;”CCXCVIII”;”CCXCIX”;”CCC”}

We have limited the example to 300 as Excel cannot display more than 8,192 characters when processing  a Function using F9.

Stepping out one more function

=MATCH(A2,INDEX(ROMAN(ROW(INDIRECT(“1:3999”))),0),0)

The Excel Index() function is taking the Array of Roman Numerals and Converting it into a single Column array

This isn’t technically needed but it simplifies the solution

If you goto cell D11 in the sample file =INDEX(ROMAN(ROW(INDIRECT(“1:3999”))),0) press F2 and then F9

Excel returns an array of roman numbers

={“I”;”II”;”III”;”IV”;”V”;”VI”;”VII”;”VIII”; … “CCXCVI”;”CCXCVII”;”CCXCVIII”;”CCXCIX”;”CCC”}

This is exactly the same as the previous output from the Roman() function above, Except that it is now a Single Vertical Array.  This is important for the next function.

Stepping out one more function

=MATCH(A2,INDEX(ROMAN(ROW(INDIRECT(“1:3999”))),0),0)

We can see here that the Array of Roman Numerals is now being fed into a Match() function.

Match uses the Syntax =Match(Lookup value, Lookup Array, Match Type)

Match returns the position of the Lookup value within the array

So in our example

The Lookup value is A2 or our Roman Numeral MMMCCCLVII

The Lookup Array is an array of Roman Numerals from 1 to 3999

={“I”;”II”;”III”;”IV”;”V”;”VI”;”VII”;”VIII”; … “MMMCMXCV”; “MMMCMXCVI”; “MMMCMXCVII”; “MMMCMXCVIII”; “MMMCMXCIX”}

and the Match Type is 0 or an exact Match

So the Match function will lookup the value MMMCCCLVII in the array and find it in position number 3357, which happens to correspond to the Number of the Roman Numeral and Return 3357 as the result.

### Why are we limited to 3999 numbers.

The Excel Roman() function is limited to numbers up to 3999 Why 3999?

Because in Roman Numerals there is no Letter for 5,000 and 4,000 would be shown as M before the Letter for 5,000.

### Why did we need the Index() function?

If you goto D17 in the sample file you will see the formula:

=MATCH(A2,ROMAN(ROW(INDIRECT(“1:3999”))),0)

It is the same formula as above but without the Index() function

You will see that it is returning a #VALUE! error

If you edit the formula with F2 and then press F9 to process the function you will see it now shows 3357

What s happening here?

Pressing F9 is the same as Array Entering the Function

so if you edit the function pressing F2 and now Array Enter the function by pressing Ctrl+Shift+Enter, excel now returns 3357

The Index() function puts a wrapper around the array for processing by the Match() function and so Array Entering is avoided.

Most array formulas that require Ctrl+Shift+Enter can be rewritten incorporating an INDEX wrapper and will not require the Ctrl+Shift+Enter confirmation.

### The Excel Arabic Function

In 2013, Microsoft introduced the Arabic function to Excel

To use simply use =Arabic(A2) or =Arabic(“MMMCCCLVII”)

You can read about the Syntax of the function in the Excel Help.

## A Challenge

Can you solve the problem another way ?

## Other Posts in this Series

The Formula Forensics Series contains a wealth of useful solutions and information specifically about how Normal Formula and specifically Array Formula work.

If you have a formula and you want to understand how it works contact Hui and it may be featured in future posts.

### Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

### Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Excel School made me great at work.
5/5

– Brenda

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

## Related Tips

### 6 Responses to “Formula Forensics No. 041 – Convert a Roman Numeral to a Number”

1. PINOY-exceler says:

shortened it a bit but same principle

=SUMPRODUCT(MATCH(A2,ROMAN(ROW(1:3999),0),0))

• PINOY-exceler says:

minus the sumproduct
entered as an array

=MATCH(A2,ROMAN(ROW(1:3999),0),0)

• Luke M says:

Minus the optional ROMAN argument.

=MATCH(A2,ROMAN(ROW(1:3999)),0)

2. XLSTIME says:

Thank you Chandoo for publishing this.

3. Herbert says:

Hi Chandoo,
Great post.
Question: why would you include the INDIRECT function in the formula? I tried =MATCH(A2,INDEX(ROMAN(ROW(1:3999)),0),0) and this gives the same result.

Regards,
Herbert

4. Lori says:

The formula from the post is remarkably similar to one i posted back in 2007:
Interestingly, it actually seems to run slightly faster and use less memory with the additional INDEX() function inserted since the array formulas have a small overhead associated so i agree with Hui that it is probably a good idea imo remove CSE.

Another possibility which is longer but significantly more efficient:
=SUMPRODUCT(LEN(C1)-LEN(SUBSTITUTE(C1,{"M","D","C","L","X","V","I";"CM","CD","XC","XL","IX","IV",0},"")),
{1000,500,100,50,10,5,1;-100,-100,-10,-10,-1,-1,0})
Credit is due to Harlan Grove from which it was adapted.

As chandoo says, the ARABIC function is definitely the way to go in 2013 version and above.

### Get FREE Excel & Power-BI Newsletter

One email per week with Excel and Power BI goodness. Join 100,000+ others and get it free.