+ Reply to Thread
Results 1 to 7 of 7

Function for Roman Numerals

  1. #1
    Gary's Student
    Guest

    Function for Roman Numerals

    What function is the inverse of =ROMAN()? That is, what function returns a
    numeric value given a Roman numeral as its argument?
    --
    Gary's Student

  2. #2
    Bernie Deitrick
    Guest

    Re: Function for Roman Numerals

    No. You need to use a UDF. See the code below, from a post by Dave
    Peterson (who couldn't remember the original source).

    HTH,
    Bernie
    MS Excel MVP


    Option Explicit
    Function Arabic(Roman)
    'Declare variables
    Dim Arabicvalues() As Integer
    Dim convertedvalue As Long
    Dim currentchar As String * 1
    Dim i As Integer
    Dim message As String
    Dim numchars As Integer

    'Trim argument, get argument length, and redimension array
    Roman = LTrim(RTrim(Roman))
    numchars = Len(Roman)
    If numchars = 0 Then 'if arg is null, we're outta here
    Arabic = ""
    Exit Function
    End If

    ReDim Arabicvalues(numchars)
    'Convert each Roman character to its Arabic equivalent
    'If any character is invalid, display message and exit
    For i = 1 To numchars
    currentchar = Mid(Roman, i, 1)
    Select Case UCase(currentchar)
    Case "M": Arabicvalues(i) = 1000
    Case "D": Arabicvalues(i) = 500
    Case "C": Arabicvalues(i) = 100
    Case "L": Arabicvalues(i) = 50
    Case "X": Arabicvalues(i) = 10
    Case "V": Arabicvalues(i) = 5
    Case "I": Arabicvalues(i) = 1
    Case Else
    Arabic = "Sorry, " & Roman & " is not a valid Roman numeral!
    "
    Exit Function
    End Select
    Next i

    'If any value is less than its neighbor to the right,
    'make that value negative
    For i = 1 To numchars - 1
    If Arabicvalues(i) < Arabicvalues(i + 1) Then
    Arabicvalues(i) = Arabicvalues(i) * -1
    End If
    Next i
    'Build Arabic total
    For i = 1 To numchars
    Arabic = Arabic + Arabicvalues(i)
    Next i

    End Function


    "Gary's Student" <[email protected]> wrote in message
    news:[email protected]...
    > What function is the inverse of =ROMAN()? That is, what function returns

    a
    > numeric value given a Roman numeral as its argument?
    > --
    > Gary's Student




  3. #3
    JE McGimpsey
    Guest

    Re: Function for Roman Numerals

    Searching the archives,

    http://groups.google.com/advanced_gr...ugroup=*excel*

    Dave Peterson posted a UDF of unknown provenance:


    http://groups-beta.google.com/group/...worksheet.func
    tions/msg/63126e7ebd85d9e5?hl=en



    In article <[email protected]>,
    "Gary's Student" <[email protected]> wrote:

    > What function is the inverse of =ROMAN()? That is, what function returns a
    > numeric value given a Roman numeral as its argument?


  4. #4
    Bernie Deitrick
    Guest

    Re: Function for Roman Numerals

    And just to get this into the archives, here's another UDF, apparently by
    Laurent Longre.

    HTH,
    Bernie
    MS Excel MVP

    '----------------------------------------------------------------------
    ' Conversion d'un nombre < 4000 en chiffres romains (style "classique")
    ' vers un nombre en chiffres arabes
    '----------------------------------------------------------------------
    'Laurent Longre, mpfe

    Function ROMINVERSE(Nombre As String)

    Const Symb = "IVXLCDM"
    Dim I As Integer, J As Integer
    Dim K As Integer, L As Integer, S As Integer
    Dim C As String * 1, Prec As Boolean

    On Error GoTo Erreur
    I = Len(Nombre)
    Do
    K = InStr(1, Symb, Mid$(Nombre, I, 1))
    If K = 0 Or K = J Then Err.Raise xlErrValue
    S = IIf(K Mod 2, 1, 5) * 10 ^ ((K - 1) \ 2)
    If K < J Then
    If Not Prec Then Err.Raise xlErrValue
    Select Case Mid$(Nombre, I, 2)
    Case Is = "ID", Is = "IM", Is = "VX", Is = "VD", _
    Is = "VM", Is = "LC", Is = "DM"
    Err.Raise xlErrValue
    End Select
    ROMINVERSE = ROMINVERSE - S
    I = I - 1
    Prec = False
    ElseIf K Mod 2 Then
    C = Mid$(Symb, K, 1)
    L = 0
    Do
    If Mid$(Nombre, I, 1) = C Then
    If L = 3 Then Err.Raise xlErrValue
    ROMINVERSE = ROMINVERSE + S
    I = I - 1
    L = L + 1
    Else
    Prec = L = 1
    Exit Do
    End If
    Loop While I
    Else
    ROMINVERSE = ROMINVERSE + S
    I = I - 1
    Prec = True
    End If
    J = K
    Loop While I
    Exit Function

    Erreur:
    ROMINVERSE = CVErr(Err)

    End Function



    "Gary's Student" <[email protected]> wrote in message
    news:[email protected]...
    > What function is the inverse of =ROMAN()? That is, what function returns

    a
    > numeric value given a Roman numeral as its argument?
    > --
    > Gary's Student




  5. #5
    Harlan Grove
    Guest

    Re: Function for Roman Numerals

    Bernie Deitrick wrote...
    >No. You need to use a UDF. . . .


    No you don't. This can be done with formulas, and not terribly complex
    ones.

    =SUMPRODUCT(LEN(x)-LEN(SUBSTITUTE(x,{"M";"D";"C";"L";"X";"V";"I"},"")),
    {1000;500;100;50;10;5;1})
    +SUMPRODUCT(LEN(C1)-LEN(SUBSTITUTE(C1,{"CM";"CD";"XC";"XL";"IX";"IV";0},"")),
    {-100;-100;-10;-10;-1;-1;0})


  6. #6
    Bernie Deitrick
    Guest

    Re: Function for Roman Numerals

    Harlan,

    Then I should have been clearer: "No, there is no built-in Excel function,
    but you can use ....."

    HTH,
    Bernie
    MS Excel MVP


    "Harlan Grove" <[email protected]> wrote in message
    news:[email protected]...
    > Bernie Deitrick wrote...
    > >No. You need to use a UDF. . . .

    >
    > No you don't. This can be done with formulas, and not terribly complex
    > ones.
    >
    > =SUMPRODUCT(LEN(x)-LEN(SUBSTITUTE(x,{"M";"D";"C";"L";"X";"V";"I"},"")),
    > {1000;500;100;50;10;5;1})
    >

    +SUMPRODUCT(LEN(C1)-LEN(SUBSTITUTE(C1,{"CM";"CD";"XC";"XL";"IX";"IV";0},""))
    ,
    > {-100;-100;-10;-10;-1;-1;0})
    >




  7. #7
    Harlan Grove
    Guest

    Re: Function for Roman Numerals

    Bernie Deitrick wrote...
    >Then I should have been clearer: "No, there is no built-in Excel

    function,
    >but you can use ....."

    ....

    OK, but there's a trade-off between udfs and long formulas. Formulas
    need to be really long, really complicated and process LOTS of data
    before they become anywhere near as slow as udfs. And formulas using
    only built-in functions don't cause problems with macro security
    settings.


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1