Another VB Question

Charlie

Board Regular
Joined
Mar 1, 2002
Messages
134
Could Tom or one of the other VB gurus here look at this code and tell me whats wrong.
I just cant get it to work.

Private Sub cmdQUIT_Click()
'Terminate Application
End
End Sub

Private Sub cmdStart_Click()
'Declare required storage
Dim Result As Currency
Dim PenceResult As Currency
Dim Required As Currency
Dim Payment As Currency


'Ask user to input required amount
Required = InputBox("Please input the cost amount ", "Cost Amount")
Payment = InputBox("Please input the amount paid ", "Amount Paid")

Result = Payment - Required

'put pounds into pence
PenceResult = Result * 100

'£5 notes
X = PenceResult Mod 500
If Result > 0 Then
picMessage.Print X & " £5 notes in change"
PenceResult = PenceResult - (X * 500)
End If

'£2 coins
X = PenceResult Mod 200
If Result > 0 Then
picMessage.Print X & " £2 coins in change"
PenceResult = PenceResult - (X * 200)
End If

'£1 coins
X = PenceResult Mod 100
If Result > 0 Then
picMessage.Print X & " £1 coins in change"
PenceResult = PenceResult - (X * 100)
End If
'50p coins
X = PenceResult Mod 50
If Result > 0 Then
picMessage.Print X & " 50p coins in change"
PenceResult = PenceResult - (X * 50)
End If
'20p coins
X = PenceResult Mod 20
If Result > 0 Then
picMessage.Print X & " 20p coins in change"
PenceResult = PenceResult - (X * 20)
End If
'10p Coins
X = PenceResult Mod 10
If Result > 0 Then
picMessage.Print X & " 10p coins in change"
PenceResult = PenceResult - (X * 10)
End If
'5p Coins
X = PenceResult Mod 5
If Result > 0 Then
picMessage.Print X & " 5p coins in change"
PenceResult = PenceResult - (X * 5)
End If
'2p Coins
X = PenceResult Mod 2
If Result > 0 Then
picMessage.Print X & " 2p coins in change"
PenceResult = PenceResult - (X * 2)
End If

'1p Coins
X = PenceResult Mod 1
If Result > 0 Then
picMessage.Print X & " 1p coins in change"
PenceResult = PenceResult - (X * 1)
End If
End Sub

Cheers
Charlie
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
It seems to me you have got a bit confused by Mod, which returns the remainder after division. So if PenceResult is 600:

X = PenceResult Mod 500

returns 100, which is not the number of £5 notes you need.

Rather it is:

X = Int(PenceResult / 500)

Then:

PenceResult = PenceResult Mod 500

to carry forward the remainder.
 
Upvote 0
Hi Charlie.
This will loop, plucking out the largest to smallest denomination on each iteration.<pre>
Private Sub cmdStart_Click()
'Declare required storage
Dim Result As Currency
Dim PenceResult As Currency
Dim Required As Currency
Dim Payment As Currency
Dim x As Long


'Ask user to input required amount
Required = InputBox("Please input the cost amount ", "Cost Amount")
Payment = InputBox("Please input the amount paid ", "Amount Paid")

Result = Payment - Required

'put pounds into pence
PenceResult = Result * 100

Do Until PenceResult = 0

Select Case PenceResult

'£5 notes
Case Is >= 500
x = Int(PenceResult / 500)
PenceResult = PenceResult - (x * 500)
picMessage.Print x & " £5 notes in change"

'£2 coins
Case Is >= 200
x = Int(PenceResult / 200)
PenceResult = PenceResult - (x * 200)
picMessage.Print x & " £2 coins in change"

'£1 coins
Case Is >= 100
x = Int(PenceResult / 100)
PenceResult = PenceResult - (x * 100)
picMessage.Print x & " £1 coins in change"

'50p coins
Case Is >= 50
x = Int(PenceResult / 50)
PenceResult = PenceResult - (x * 50)
picMessage.Print x & " 50p coins in change"

'20p coins
Case Is >= 20
x = Int(PenceResult / 20)
PenceResult = PenceResult - (x * 20)
picMessage.Print x & " 20p coins in change"

'10p coins
Case Is >= 10
x = Int(PenceResult / 10)
PenceResult = PenceResult - (x * 10)
picMessage.Print x & " 10p coins in change"

'5p coins
Case Is >= 5
x = Int(PenceResult / 5)
PenceResult = PenceResult - (x * 5)
picMessage.Print x & " 5p coins in change"

'2p coins
Case Is >= 2
x = Int(PenceResult / 2)
PenceResult = PenceResult - (x * 2)
picMessage.Print x & " 2p coins in change"

'1p coins
Case Is >= 1
x = Int(PenceResult / 1)
PenceResult = PenceResult - (x * 1)
picMessage.Print x & " 1p coins in change"

End Select

Loop

End Sub</pre>
Tom

I see Andrew's similiar reply. You could replace the minus sign in each case with MOD and get the same results.
Or
PenceResult = PenceResult MOD 500
PenceResult = PenceResult MOD 200
ect...
This message was edited by TsTom on 2002-10-14 16:02
 
Upvote 0
Brilliant as usual...I wish I was as good as you guys here.
Maybe some day I could actually help someone myself.
Thanks guys
Charlie
 
Upvote 0
So if I declare X As Integer would this have the same effect as placing Int in front of(PenceResult/500).
Charlie
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top