VBA Query

2016LM

New Member
Joined
Sep 9, 2016
Messages
18
Hi, I have a simple piece of code that calculates the mass of a gas at a known pressure within a fixed volume (please see below). However, when I run said code, I get a 'Run-time error '6': Overflow' error. When I scroll the curser across the offending line (i.e. m(1) =...) the last bit in brackets (i.e. ...(R * T * Z)) shows up as '(R * T * Z) = <Overflow>'. After several hours of head scratching, I found that if I slightly changed the ordering to (R * Z * T) i.e. reverse the T and Z, the code works.

Public Const V = 20
Public Const atm = 1.013
Public Const Mwt = 28.01
Public Const R = 8314
Public Const T = 278
Sub Example()
Dim P(1 To 2), m(1 To 2) As Variant

P(1) = 0
Z = 0.9991
m(1) = (P(1) + atm) * (10 ^ 5) * V * Mwt / (R * T * Z)

ActiveCell.Value = m(1)
End Sub


I'm not the most proficient VBA user (so I'm guessing the reason is obvious), but I am at a complete loss as to why reversing the Z and T variables would make a difference.

Any help / assistance / enlightenment anyone could provide would be much appreciated.

Kind regards,

2016LM
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
The problem is with the R*T portion, I suspect that VBA is trying to assign that as an integer, but is you do R*Z its probably assigned as Double (as Z is a decimal)
If you make this change it should work
Code:
Public Const R As Long = 8314
 
Upvote 0
Hi Fluff,

Ah, that makes sense. Code works fine now, thanks for taking the time to review and reply with the answer, I really appreciate it.

Kind regards,
2016Lm
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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