A Spell Number macro converts a numeric value into its written English equivalent, such as turning 103.50 into "One Hundred Three Dollars and Fifty Cents". This is most commonly used in Microsoft Excel or Access to generate invoices, checks, or formal financial reports. You do not need any special software; the process relies on the built-in VBA (Visual Basic for Applications) editor found in all Microsoft Office applications. The following guide provides the code and the steps to implement it reliably.
What You'll Need
- A computer running Microsoft Windows.
- Microsoft Excel, Access, or Word installed (the code works across all Office applications).
- A basic understanding of keyboard shortcuts (Alt+F11 to open the editor).
- A blank workbook or database to test the macro.
Step-by-Step
- Open the Developer Tab. If you are in Excel, navigate to the top menu and click File > Options > Customize Ribbon. In the right-hand pane, check the box next to Developer and click OK. This tab provides the tools needed to write and run your code.
- Open the VBA Editor. Once the Developer tab is visible, click the Visual Basic icon. Alternatively, press Alt + F11 on your keyboard. This opens the Integrated Development Environment (IDE) where you will write the macro.
- Create a New Module. In the left-hand pane labeled "Project - VBAProject", right-click on the specific project name for your workbook (e.g., VBAProject (Book1)). Select Insert > Module. A blank white window will appear on the right side.
- Paste the Spell Number Code. Copy the complete VBA code block provided below. Once copied, open the blank module window you just created and paste the code into the white area. Ensure you paste it between the
Sub SpellNumber()and the finalEnd Sublines. - Close the Editor. Click the X in the top right corner of the VBA editor window or press Alt + Q to return to your spreadsheet.
- Run the Macro. You can run the macro in two ways. The easiest is to press Alt + F8, select SpellNumber from the list, and click Run. Alternatively, go to the Developer tab and click Macros, select the macro, and run it.
- Assign to a Button (Optional). To make the macro available directly on your spreadsheet without pressing keys, go to the Developer tab and click Insert. Choose Button (Form Control) from the dropdown. Draw a button on your sheet. When the Assign Macro window pops up, select SpellNumber and click OK.
- Use the Function in Cells. With the code pasted and the macro running, you can now type a number into a cell. To convert it to text, type
=SpellNumber(A1)into an adjacent cell, replacing A1 with the cell containing your number. Press Enter, and the cell will display the written text.
The VBA Code
Copy the following code exactly as it appears. It handles integers, decimals, and currency formatting automatically.
Function SpellNumber(ByVal MyNumber)
Dim Dollars, Cents, Temp
Dim DecimalPlace, Count
ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "
MyNumber = Trim(Str(MyNumber))
DecimalPlace = InStr(MyNumber, ".")
If DecimalPlace > 0 Then
Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber >> 0
Temp = GetTens(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) >= 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
Select Case Dollars
Case ""
Dollars = "Zero Dollars"
Case Else
Dollars = Dollars & " Dollars"
End Select
SpellNumber = Dollars & " and " & Cents & " Cents"
End Function
Function GetTens(TensText)
Dim Result As String
Result = ""
If Val(Left(TensText, 1)) = 1 Then
Select Case Val(TensText)
Case 10: Result = "Ten"
Case 11: Result = "Eleven"
Case 12: Result = "Twelve"
Case 13: Result = "Thirteen"
Case 14: Result = "Fourteen"
Case 15: Result = "Fifteen"
Case 16: Result = "Sixteen"
Case 17: Result = "Seventeen"
Case 18: Result = "Eighteen"
Case 19: Result = "Nineteen"
End Select
Else
Select Case Val(Left(TensText, 1))
Case 2: Result = "Twenty "
Case 3: Result = "Thirty "
Case 4: Result = "Forty "
Case 5: Result = "Fifty "
Case 6: Result = "Sixty "
Case 7: Result = "Seventy "
Case 8: Result = "Eighty "
Case 9: Result = "Ninety "
End Select
Result = Result & GetTens(Right(TensText, 1))
End If
GetTens = Result
End Function
Common Mistakes
- Not saving the Workbook as Macro-Enabled. If you simply save your Excel file as a standard .xlsx, the macro will disappear when you close the file. You must save it as a Macro-Enabled Workbook (*.xlsm). This is the most common reason the function fails to work after a refresh.
- Typing the formula incorrectly. The function name is SpellNumber, not spellnumber or Spell_Number. Excel is case-insensitive, but spelling must be exact. Also, ensure you do not put parentheses around the cell reference, like
=SpellNumber(A1). It should be=SpellNumber(A1). - Editing the code while the Macro is running. If you click the Play button or press F5 while the code is executing, the editor window will pop up and interrupt the process. You must wait for the function to finish calculating before you make any changes to the code.
Tips
- Formatting the Output. The code above returns the result as standard text. If you want the text to look like currency, you can wrap the function in a formula. For example,
="$" & SpellNumber(A1)will add the dollar sign automatically. - Removing Decimal Places. If you are working with whole numbers only, you can modify the code to ignore the cents section. However, the provided code is robust and handles both scenarios without needing modification.
- Large Numbers. This specific script supports numbers up to Billions. If you need to convert Trillions or higher, you would need to add a new entry to the
Placearray and adjust the looping logic, though this is rarely necessary for standard business documents.
Troubleshooting
If the macro does not work as expected, follow this order of checks to isolate the problem.
1. Check the File Format. As mentioned in the Common Mistakes section, the file must be saved as an .xlsm file. If you have a standard Excel sheet, the macro is inactive. Go to File > Save As and select the format.
2. Verify the Function Name. If you see a #NAME? error in the cell, it means Excel does not recognize the function name. Double-check the spelling in the code and the formula. Ensure the code is pasted into a Module and not inside a Sheet or ThisWorkbook object.
3. Ensure the Code is Public. If you are using this in Access or if you are writing the code inside a specific Object rather than a Module, the function must be declared as Public. In the code provided, the line Function SpellNumber(...) implies public scope by default, but if you moved it, ensure it is not marked Private.
4. Clear Cached Data. Sometimes Excel caches old versions of functions. To force a recalculation, press Ctrl + Alt + F9. This calculates all formulas in the workbook, including the Spell Number function, regardless of whether the source numbers have changed.
5. Check for Conflicts. If you have multiple macros named SpellNumber in different modules, Excel may run the wrong one. Ensure there are no duplicate macro names in your VBA project.
Frequently Asked Questions
- Can I use this macro in Google Sheets? No, the VBA code provided is specific to Microsoft Excel and Access. Google Sheets does not support VBA. You would need to use a Google Apps Script written in JavaScript to achieve the same result, or use a third-party add-on from the G Suite Marketplace.
- Why does the macro say "Zero Dollars"? This usually happens if the cell referenced contains text or is empty. The macro function expects a numeric value. Ensure the cell containing the number is formatted as a Number or General type, and not as Text.
- How do I make the macro run automatically when I type a number?
You can use the Worksheet_Change event. This requires editing the VBA code again. You would add a small script that detects when a value is entered in a specific column and automatically triggers the
SpellNumberfunction on the adjacent column. - Is it safe to use custom macros? Yes, provided you copy the code from a trusted source and do not run code from unknown senders. The code provided above is a standard, open-source algorithm used for invoice generation and does not access your personal files or internet connection.
- Can I change the words "Dollars" and "Cents"?
Yes. Locate the lines
Case "" Dollars = "Zero Dollars"andSpellNumber = Dollars & " and " & Cents & " Cents"in the code. Simply change "Dollars" to "Euros" or "Rupees" and "Cents" to "Pence" to match your currency. - Does this work with negative numbers? The basic version of this script does not handle negative numbers gracefully, often resulting in "Minus Zero Dollars". To support negatives, you would need to add a check at the very beginning of the function to detect a negative sign and adjust the output logic accordingly.