OFF98: VBA Macro to Convert Numbers to Roman Numerals

ID: Q185178


The information in this article applies to:
  • Microsoft Office 98 Macintosh Edition


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/overview/overview.asp

Example

  1. Start any Office program.


  2. Start the Visual Basic Editor (press Option+F11).


  3. On the Insert menu, click Module.


  4. 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 


  5. On the View menu, click Immediate Window.


  6. Type the following into the Immediate window and press RETURN.
    ?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:

Q163435 VBA: Programming Resources for Visual Basic for Applications

Additional query words: XL98 WD98 PPT OFF98

Keywords : kbdta EPUCon xlvbahowto OffVBA
Version : MACINTOSH:98
Platform : MACINTOSH
Issue type : kbhowto


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.