OFF97: VBA Procedure to Convert Numbers to Roman Numerals (184657)



The information in this article applies to:

  • Microsoft Office 97 for Windows

This article was previously published under Q184657

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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.

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:

163435 VBA: Programming Resources for Visual Basic for Applications


Modification Type:MajorLast Reviewed:6/18/2005
Keywords:kbinfo KB184657