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
As always at Formula Forensics you can follow along using a sample file: Download Sample File
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.
Download
You can download a copy of the above file and follow along, Download Sample File.
A Challenge
Can you solve the problem another way ?
Post your solutions in the comments below.
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.
You can learn more about how to pull Excel Formulas apart in the following posts: http://chandoo.org/wp/formula-forensics-homepage/
If you have a formula and you want to understand how it works contact Hui and it may be featured in future posts.




















6 Responses to “Make VBA String Comparisons Case In-sensitive [Quick Tip]”
Another way to test if Target.Value equal a string constant without regard to letter casing is to use the StrCmp function...
If StrComp("yes", Target.Value, vbTextCompare) = 0 Then
' Do something
End If
That's a cool way to compare. i just converted my values to strings and used the above code to compare. worked nicely
Thanks!
In case that option just needs to be used for a single comparison, you could use
If InStr(1, "yes", Target.Value, vbTextCompare) Then
'do something
End If
as well.
Nice tip, thanks! I never even thought to think there might be an easier way.
Regarding Chronology of VB in general, the Option Compare pragma appears at the very beginning of VB, way before classes and objects arrive (with VB6 - around 2000).
Today StrComp() and InStr() function offers a more local way to compare, fully object, thus more consistent with object programming (even if VB is still interpreted).
My only question here is : "what if you want to binary compare locally with re-entering functions or concurrency (with events) ?". This will lead to a real nightmare and probably a big nasty mess to debug.
By the way, congrats for you Millions/month visits 🙂
This is nice article.
I used these examples to help my understanding. Even Instr is similar to Find but it can be case sensitive and also case insensitive.
Hope the examples below help.
Public Sub CaseSensitive2()
If InStr(1, "Look in this string", "look", vbBinaryCompare) = 0 Then
MsgBox "woops, no match"
Else
MsgBox "at least one match"
End If
End Sub
Public Sub CaseSensitive()
If InStr("Look in this string", "look") = 0 Then
MsgBox "woops, no match"
Else
MsgBox "at least one match"
End If
End Sub
Public Sub NotCaseSensitive()
'doing alot of case insensitive searching and whatnot, you can put Option Compare Text
If InStr(1, "Look in this string", "look", vbTextCompare) = 0 Then
MsgBox "woops, no match"
Else
MsgBox "at least one match"
End If
End Sub