InputBox and MsgBox with percentage

matthewlrx

New Member
Joined
Jul 4, 2022
Messages
16
Office Version
  1. 2021
Platform
  1. Windows
Hi all

I have written the below code to work out what a percentage is of certain scores. The strange thing is the output box is showing the percentage wrong.

If i take the following:

Yes Responses: 118
No Responses: 10
Repeated Responses: 0

My formula to calculate percentage would be (Yes-Repeat)/(Yes+No)*100 and it should give me 92.19% but my vba gives me 1%.

What am I doing wrong here?

VBA Code:
Sub RAP()

Dim Yes As Variant
Yes = InputBox("How Many Yes's do you have", "RAP Calculator")

Dim No As Variant
No = InputBox("How Many No's do you have", "RAP Calculator")

Dim Repeat As Variant
Repeat = InputBox("How Many Repeat's do you have", "RAP Calculator")

Dim PRR As Variant
PRR = (Yes - Repeat)

Dim HMD As Variant
HMD = (Yes + No)

Dim RAP As Variant
RAP = (PRR / HMD) * 100


MsgBox "The Required Percentage is: " + Format(RAP, "0.00") + "%"

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try this:
VBA Code:
Sub RAP()

Dim Yes As Long
Yes = InputBox("How Many Yes's do you have", "RAP Calculator")

Dim No As Long
No = InputBox("How Many No's do you have", "RAP Calculator")

Dim Repeat As Long
Repeat = InputBox("How Many Repeat's do you have", "RAP Calculator")

Dim PRR As Long
PRR = (Yes - Repeat)

Dim HMD As Long
HMD = (Yes + No)

Dim RAP As Double
RAP = (PRR / HMD)

MsgBox "The Required Percentage is: " & Format(RAP, "0.00%")

End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Sub RAP()

Dim Yes As Long
Yes = InputBox("How Many Yes's do you have", "RAP Calculator")

Dim No As Long
No = InputBox("How Many No's do you have", "RAP Calculator")

Dim Repeat As Long
Repeat = InputBox("How Many Repeat's do you have", "RAP Calculator")

Dim PRR As Long
PRR = (Yes - Repeat)

Dim HMD As Long
HMD = (Yes + No)

Dim RAP As Double
RAP = (PRR / HMD)

MsgBox "The Required Percentage is: " & Format(RAP, "0.00%")

End Sub
Many thanks, that worked perfectly
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,944
Members
449,095
Latest member
nmaske

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