VBA Userform not calculating correctly

TMal830

New Member
Joined
Sep 24, 2013
Messages
12
Hello,

I'm trying to figure out how much freight I am paying per sq ft of a product I sell. I'm entering all information into a userform. I enter the "Freight" which is the total freight I'm paying, the "sqftsum" is the total amount of square feet, and the "frsqft" is the freight per square foot that needs to be calculated. When I use the code below, assuming the total freight was $5 for a total of 173.45 square feet, the total it gives me is 2.88267512251. It should be 0.0288267512251. No idea why it's moving the decimal place over 2 spots, and if I try dividing the answer by 100 nothing happens. Hoping someone can help me make sense of this! Any help would be appreciated. Thanks.

If Not Freight = "" Then
x = 1
sqftsum = 0
Do
sqftsum = sqftsum + ListBox1.List(x, 3)
x = x + 1
frsqft = (Freight / sqftsum)
Loop Until x = c
End If
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
It's hard to see everything you have going on here, but I suspect the issue may be caused by your calculating frsqft after each increment of sqftsum. Try moving this line:

Code:
frsqft = (Freight / sqftsum)

to after this line:

Code:
Loop Until x = c

Regards,

CJ
 
Upvote 0
It's hard to see everything you have going on here, but I suspect the issue may be caused by your calculating frsqft after each increment of sqftsum. Try moving this line:

Code:
frsqft = (Freight / sqftsum)

to after this line:

Code:
Loop Until x = c

Regards,

CJ

That didn't work, but I found something pretty interesting. Even if I remove the variables and replace them with the actual value, I step through (f8) and the value still comes up with the decimal point moved over 2 spaces. What's even weirder is that I can change the "5" to "500" and still come up with the exact same value. Does the same happen to you if you just use this one line in your code?

c = (5 / 173.45)
c = (500 / 173.45)
 
Upvote 0
Probably more a display issue as it comes out for me in debug.print as 2.88267512251369E-02 which is as correct as Excel gets.

Code:
Sub test1()
Dim frsqft As Double, sqftsum As Double, Freight As Long
Freight = 5
sqftsum = 173.45
frsqft = (Freight / sqftsum)
Debug.Print frsqft
End Sub
 
Last edited:
Upvote 0
That's the problem though, it moved over the decimal point 2 spaces. 5 divided by 173.45 should be 0.0288267512251. If you change that "5" to "500" you get the exact same answer for some reason. Anyone have any ideas?
 
Upvote 0
E-02 is scientific notation for the number being 2 decimal places across.

I see there is a problem with the 500 but the below gives the correct answer with E-04 showing.
Code:
Sub test1()
Dim frsqft As Double, sqftsum As Double, Freight As Long
Freight = 5
sqftsum = 173.45
frsqft = (Freight / sqftsum) / 100
Debug.Print frsqft
End Sub
 
Last edited:
Upvote 0
Thanks so much for the info. The scientific notation is new to me so it wasn't clicking for me. Thanks again!
 
Upvote 0
You could get around the scientific notation by using Round:

Code:
frsqft = Round(Freight / sqftsum,5)

CJ
 
Upvote 0
You could get around the scientific notation by using Round:

Code:
frsqft = Round(Freight / sqftsum,5)

CJ

It doesn't fix the issue with using 500 as the variable unfortunately...

Code:
Sub test1()
Dim frsqft As Double, sqftsum As Double, Freight As Long
Freight = 500
sqftsum = 173.45
frsqft = Round(Freight / sqftsum, 5)
Debug.Print frsqft
End Sub

Result = 2.88268
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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