There were many people (including in these forums) provided many different ways to convert back and fort between Roman and Arabic numbers.
Here is my version if you want it short: (Don't complain, I am lazy to write full comments)
Code:Function RomanToArabic(ByVal Roman As String) As Integer '-- Convert Roman number to Arabic number '-- Return -32768 as an error flag if Roman string is invalid Dim i As Integer Dim Unit As Integer Dim OldUnit As Integer OldUnit = 1000 For i = 1 To Len(Roman) Select Case UCase(Mid$(Roman, i, 1)) Case "I": Unit = 1 Case "V": Unit = 5 Case "X": Unit = 10 Case "L": Unit = 50 Case "C": Unit = 100 Case "D": Unit = 500 Case "M": Unit = 1000 Case Else: '-- unaccepted Roman digit RomanToArabic = -32768 Exit Function End Select If Unit > OldUnit Then RomanToArabic = RomanToArabic - 2 * OldUnit RomanToArabic = RomanToArabic + Unit OldUnit = Unit Next i End FunctionCode:Function ArabicToRoman(ByVal Arabic As Integer) As String '-- Convert Arabic number to Roman nunber Const R100 = ",C,CC,CCC,CD,D,DC,DCC,DCCC,CM" Const R10 = ",X,XX,XXX,XL,L,LX,LXX,LXXX,XC" Const R1 = ",I,II,III,IV,V,VI,VII,VIII,IX" If Arabic < 0 Then '-- this is not a proper way but ArabicToRoman = "-" ' provides a way to deal with Arabic = -Arabic ' negative numbers instead of End If ' raising an error ArabicToRoman = ArabicToRoman & String$(Arabic \ 1000, "M") Arabic = Arabic Mod 1000 ArabicToRoman = ArabicToRoman & Split(R100, ",")(Arabic \ 100) Arabic = Arabic Mod 100 ArabicToRoman = ArabicToRoman & Split(R10, ",")(Arabic \ 10) Arabic = Arabic Mod 10 ArabicToRoman = ArabicToRoman & Split(R1, ",")(Arabic) End Function




Reply With Quote