OFF97: VBA Procedure to Convert Numbers to Roman Numerals

ID: Q184657

The information in this article applies to:

SUMMARY

Movie dates and bullet items often use Roman numerals, such as the following:

   MCMXCVIII

Visual Basic for Applications has no built-in functions that convert an integer to Roman numerals. However, you can create a custom routine that will convert an integer in the range 1 to 3999 to Roman numerals. This article shows you how to do so.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft Support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

Example

1. Start any Office program.

2. Start the Visual Basic Editor and insert a module into your project.

3. Type the following code in the module:

      Function Num2Roman (ByVal N As Integer) As String

        Const Digits = "IVXLCDM"

        Dim I As Integer, Digit As Integer, Temp As String

        I = 1
        Temp = ""
        Do While N > 0
          Digit = N Mod 10
          N = N \ 10
          Select Case Digit
            Case 1
              Temp = Mid(Digits, I, 1) & Temp
            Case 2
              Temp = Mid(Digits, I, 1) & Mid(Digits, I, 1) & Temp
            Case 3
              Temp = Mid(Digits, I, 1) & Mid(Digits, I, 1) & _
                     Mid(Digits, I, 1) & Temp
            Case 4
              Temp = Mid(Digits, I, 2) & Temp
            Case 5
              Temp = Mid(Digits, I + 1, 1) & Temp
            Case 6
              Temp = Mid(Digits, I + 1, 1) & Mid(Digits, I, 1) & Temp
            Case 7
              Temp = Mid(Digits, I + 1, 1) & Mid(Digits, I, 1) & _
                     Mid(Digits, I, 1) & Temp
            Case 8
              Temp = Mid(Digits, I + 1, 1) & Mid(Digits, I, 1) & _
                     Mid(Digits, I, 1) & Mid(Digits, I, 1) & Temp
            Case 9
              Temp = Mid(Digits, I, 1) & Mid(Digits, I + 2, 1) & Temp
          End Select
          I = I + 2
        Loop
        Num2Roman = Temp
      End Function

4. On the View menu click Immediate Window.

5. Type the following into the Immediate window and press ENTER:

      ?Num2Roman(948)

   You will see the following in the Immediate window:

      ?Num2Roman(948)
      CMXLVIII

REFERENCES

For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications

Additional query words: vba
Keywords          : kbdta EPUCon OffVBA PgmHowto KbVBA 
Version           : WINDOWS:5.0
Platform          : WINDOWS
Issue type        : kbinfo

Last Reviewed: May 18, 1999