Search code examples
excelexcel-2011vba

FormatCurrency in Excel 2011


Below is some code I extracted from a Youtube tutorial using Excel 2010. When I try to run this on Excel 2011 for the Mac, the currency outputs are all integers. Does FormatCurrency() work differently in this version of Excel? Or have I just screwed up something (again)? Thanks for any help.

Best, Tony Lima

Option Explicit
' Tony Lima, July 23, 2014

Const SCOOPPRICE As Double = 0.75
Const TIPPERCENT As Double = 0.15

' ****************************************
' Ice cream store order and print receipt
' *****************************************

Private Sub btnOrder_Click()
    Dim numScoops As Integer
    Dim scoopCost As Double
    Dim tipAmount As Double
    Dim totalCost As Double

    '*** get the number of scoops ordered
    numScoops = CInt(txtScoops.Text)
    '*** calculate cost of ice cream and tip amount
    scoopCost = numScoops * SCOOPPRICE
    tipAmount = scoopCost * TIPPERCENT
    '*** add the two to get the total cost
    totalCost = scoopCost + tipAmount

    '*** print receipt
    lstReceipt.AddItem ("Thanks for your order!")
    lstReceipt.AddItem ("")
    lstReceipt.AddItem ("You ordered " & CStr(numScoops) & " scoops of ice cream")
    lstReceipt.AddItem ("Cost of ice cream " & FormatCurrency(scoopCost, 2))
    lstReceipt.AddItem ("Tip (thank you) " & FormatCurrency(tipAmount, 2))
    lstReceipt.AddItem ("Total cost " & FormatCurrency(totalCost, 2))

End Sub

Solution

  • Code works fine in Excel 2010 on Windows 7 pc. Try using the immediate window in the VBE and pepper some debug.print statements in to see what's going on.

    Debug.print FormatCurrency(scoopCost,2) for example...
    

    Just thought you should know that it does work.