Microsoft KB Archive/185178

= OFF98: VBA Macro to Convert Numbers to Roman Numerals =

Article ID: 185178

Article Last Modified on 6/23/2005

-

APPLIES TO


 * Microsoft Office 98 for Macintosh

-



This article was previously published under Q185178





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
 Start any Office program. Start the Visual Basic Editor (press Option+F11). On the Insert menu, click Module.  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  On the View menu, click Immediate Window. Type the following into the Immediate window and press RETURN.

?Num2Roman(948)

You will see the following in the Immediate window:

?Num2Roman(948)

CMXLVIII

</li></ol>

<div class="references_section">