In Microsoft Excel, macros provide a powerful way to automate repetitive tasks and enhance productivity. One useful application of macros is to create a Number to Check converter for checks. This can be particularly helpful in financial scenarios where you need to convert numerical amounts to their corresponding written forms on checks.
In this blog, we will guide you through the process of creating a simple Excel macro that converts numbers to words for check writing purposes.
Step 1: Enable Developer Tab
Before you start, ensure that the Developer tab is visible in your Excel ribbon. If not, you can enable it by following these steps:
- Go to the ‘File’ tab.
- Select ‘Options.’
- In the Excel Options dialog box, choose ‘Customize Ribbon.’
- Check the ‘Developer’ option.
- Click ‘OK.’
Step 2: Open the Visual Basic for Applications (VBA) Editor
- In the Developer tab, click on ‘Visual Basic’ to open the VBA Editor.
- In the VBA Editor, you’ll see the VBAProject (YourWorkbookName) in the left pane.
Step 3: Insert a New Module
- Right-click on ‘VBAProject (YourWorkbookName).’
- Select ‘Insert’ and then ‘Module.’ This will add a new module to your workbook.
Step 4: Write the Macro Code
In the module, you can now write the VBA code for the conversion. Below is a simple example code:
Option Explicit
Function SpellNumber(ByVal MyNumber)
Dim Dirhams, Cents, Fils, Temp
Dim DecimalPlace, Count
Const Thousand As String = " Thousand "
Const Million As String = " Million "
Const Billion As String = " Billion "
Const Trillion As String = " Trillion "
MyNumber = Trim(Str(MyNumber))
DecimalPlace = InStr(MyNumber, ".")
If DecimalPlace > 0 Then
Cents = ConvertToFils(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dirhams = Temp & GetPlace(Count) & Dirhams
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
Select Case Dirhams
Case ""
Dirhams = "No Dirhams"
Case "One"
Dirhams = "One Dirham"
Case Else
Dirhams = Dirhams & " Dirhams"
End Select
SpellNumber = Dirhams & " "
End Function
Function ConvertToFils(ByVal number As Double) As String
Dim numStr As String
numStr = Format(number, "#,##0.00")
numStr = Replace(numStr, ",")
Dim parts As Variant
parts = Split(numStr, ".")
If UBound(parts) > 0 And parts(1) <> "00" Then
ConvertToFils = " and Fils " & parts(1) & "/100 Only"
Else
ConvertToFils = "Only"
End If
End Function
Function GetHundreds(ByVal MyNumber)
Dim result As String
If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right("000" & MyNumber, 3)
If Mid(MyNumber, 1, 1) <> "0" Then
result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If
If Mid(MyNumber, 2, 1) <> "0" Then
result = result & GetTens(Mid(MyNumber, 2))
Else
result = result & GetDigit(Mid(MyNumber, 3))
End If
GetHundreds = result
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"
Case Else
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 "
Case Else
End Select
result = result & GetDigit(Right(TensText, 1))
End If
GetTens = result
End Function
Function GetDigit(Digit)
Select Case Val(Digit)
Case 1: GetDigit = "One"
Case 2: GetDigit = "Two"
Case 3: GetDigit = "Three"
Case 4: GetDigit = "Four"
Case 5: GetDigit = "Five"
Case 6: GetDigit = "Six"
Case 7: GetDigit = "Seven"
Case 8: GetDigit = "Eight"
Case 9: GetDigit = "Nine"
Case Else: GetDigit = ""
End Select
End Function
Function GetPlace(PlaceCount)
Select Case PlaceCount
Case 2: GetPlace = " Thousand "
Case 3: GetPlace = " Million "
Case 4: GetPlace = " Billion "
Case 5: GetPlace = " Trillion "
Case Else: GetPlace = ""
End Select
End Function
Step 5: Implement the Macro in Excel
- Go to the cell where you want to display the written form of a number.
- Enter the following formula:
=ConvertToWords(A1)
=CONVERTTOFILS(H6)
Replace A1 with the reference to the cell containing the numerical amount.
Now, whenever you input a number in the specified cell, the adjacent cell will display the corresponding written form, suitable for check writing.
Creating a Number to Check converter in Excel using macros provides a convenient way to automate the process of converting numerical amounts to words. This can be especially useful in financial and accounting scenarios, saving time and reducing the risk of errors in check writing.