Avatar of stephenlecomptejr
stephenlecomptejrFlag for United States of America

asked on 

Most efficient VBA function to display set of dollar bills given back to a customer like a cash register?

Four months back I had an interview programming aptitude test where I had to produce a C# program that would act like a cash register and given any amount produce how many ten, fives, one dollar bills with change as the result.

I got stressed and wanted to do it via VBA first and then I could've done it in C# but they wouldn't let for obvious reasons.  Then when I came home, nothing I tried worked in VBA cause I was still so disappointed.

So tonight I got it working.  The time it took me to complete is in the code below.

My main specific question - is there a more efficient way to do what I did below?  (I also included hundreds, fifties, twenties also but wasn't the original requirement).  

Will this function work for any combination of values submitted?  Does it truly work?

How would you have done it?  Thank you for any feedback.

Option Compare Database
Option Explicit

'1st phase-
'2/17/2020
'started:  9:37 pm
'ended:  10:21 p.m.
'
'2nd phase -
'6/18/2020-
'started:  12:30 am.
'completed:  1:13 a.m.
'
'sample tries:

'Debug.Print DollarValues("28")
'? DollarValues("$21,457.03")

Public Function DollarValues(sValue As String) As String

  Dim sNewValue As String
  '
  Dim lHundreds As Double
  Dim lFifties As Double
  Dim lTwenties As Double
  Dim lTens As Double
  Dim lFives As Double
  Dim lOnes As Double
  Dim lCents As Currency
  '
  Dim dTotalThusFar As Double
  Dim dLeftOver As Double
  Dim dFullValue As Double
  '
  'replace any symbols not necessary...
  
  sValue = Replace(sValue, "$", "")
  sValue = Replace(sValue, ",", "")
  
  dFullValue = Val(sValue)
  dLeftOver = dFullValue
  '--------------------------------------
  'determine no. of hundred dollar bills section
  If dLeftOver >= 100 Then
      lHundreds = dLeftOver / 100
      lHundreds = Int(lHundreds)
      'Debug.Print "Hundreds: " & lTens
      sNewValue = sNewValue & "Hundreds: " & lHundreds & vbNewLine
      'after number of Hundred dollar bills are identified - subtract the rest.
      dLeftOver = dLeftOver - (lHundreds * 100)
      'Debug.Print "Left over: " & dLeftOver
  End If
  '--------------------------------------
  'determine no. of fifty dollar bills section
  If dLeftOver >= 50 Then
      lFifties = dLeftOver / 50
      lFifties = Int(lFifties)
      'Debug.Print "Fifties: " & lTens
      sNewValue = sNewValue & "Fifties: " & lFifties & vbNewLine
      'after number of Fifty dollar bills are identified - subtract the rest.
      dLeftOver = dLeftOver - (lFifties * 50)
      'Debug.Print "Left over: " & dLeftOver
  End If
  '--------------------------------
  'determine no. of twenty dollar bills section
  If dLeftOver >= 20 Then
      lTwenties = dLeftOver / 20
      lTwenties = Int(lTwenties)
      'Debug.Print "Twenties: " & lTens
      sNewValue = sNewValue & "Twenties: " & lTwenties & vbNewLine
      'after number of Ten dollar bills are identified - subtract the rest.
      dLeftOver = dLeftOver - (lTwenties * 20)
      'Debug.Print "Left over: " & dLeftOver
  End If
  '--------------------------------
  'determine no. of ten dollar bills section
  If dLeftOver >= 10 Then
      lTens = dLeftOver / 10
      lTens = Int(lTens)
      'Debug.Print "Tens: " & lTens
      sNewValue = sNewValue & "Tens: " & lTens & vbNewLine
      'after number of Ten dollar bills are identified - subtract the rest.
      dLeftOver = dLeftOver - (lTens * 10)
      'Debug.Print "Left over: " & dLeftOver
  End If
  '--------------------------------
  'determine no. of five dollar bills
  If dLeftOver >= 5 Then
      lFives = dLeftOver / 5
      lFives = Int(lFives)
      'Debug.Print "Fives: " & lFives
      sNewValue = sNewValue & "Fives: " & lFives & vbNewLine
      'after number of Five dollar bills are identified - subtract the rest
      dLeftOver = dLeftOver - (lFives * 5)
      'Debug.Print "Left over: " & dLeftOver
      
  End If
  '--------------------------------
  'determine no. of one dollar bills
  If dLeftOver >= 1 Then
      'set true ones left...
      lOnes = dLeftOver
      If lOnes = Int(lOnes) Then
        'value is good.
      Else
        lOnes = Int(lOnes)
      End If
      If lOnes <> 0 Then
        'Debug.Print "Ones: " & lOnes
        sNewValue = sNewValue & "Ones: " & lOnes & vbNewLine
        'after number of one dollar bills are identified - subtract the rest
        dLeftOver = dLeftOver - (lFives * 1)
        'Debug.Print "Left over: " & dLeftOver
      End If
  End If
  '--------------------------------
  'determine no. of cents left over
  '
  'the following allows to double-check and verify the work
  'by starting over with adding all values and then subtracting the .decimal values left over.
  '
  dTotalThusFar = (lHundreds * 100) + (lFifties * 50) + (lTwenties * 20) + (lTens * 10) + (lFives * 5) + (lOnes * 1)
  If dTotalThusFar < dFullValue Then
    lCents = dFullValue - dTotalThusFar
    'Debug.Print "Cents: " & lCents
    sNewValue = sNewValue & "Cents: " & lCents
  End If

  DollarValues = sNewValue

End Function

Open in new window

ProgrammingMicrosoft AccessC#VBA

Avatar of undefined
Last Comment
stephenlecomptejr
SOLUTION
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of stephenlecomptejr

ASKER

Thank you all for your feedback thus far.  I didn't know what the proper terms to search for and I appreciate it someone checking this further.

I especially appreciate the code you provide as an example.  After I lay my head down and still thought what I had done - I noted I could reduce the code in a similiar manner.  Yours however is a little more tighter to the solution.   The only thing is where it comes to the line shown below it doesn't seem to give the correct answer.  

AValue = AValue Mod CPlaceValue

In the place of Mod - I put in \ but it still didn't arrive at the correct answers.

I'm curious as to what it should be - if that was a typo, etc.

Thanks.
Avatar of ste5an
ste5an
Flag of Germany image

Well, how did you test it?

You need to provide the number as currency in EN-US writing as it is a Currency, not a string.

User generated image
The Mod in the If is to reduce the value, so that subsequent calculations use the correct remainder.
EE29185713.xlsm
Avatar of stephenlecomptejr

ASKER

Ah okay.  That's what it was.  I was testing as a string.
Avatar of ste5an
ste5an
Flag of Germany image

I removed the string parsing, cause on my system (en-DE) it would be different than yours from above.
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of Norie
Norie

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of stephenlecomptejr

ASKER

I appreciate everyone's replies on the matter.
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo