Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stringing Together Multiple Ifs & Displaying Calculations To 2 DPs

Status
Not open for further replies.

RoccoSiffredi

Technical User
Nov 10, 2010
16
GB
I've created vba to calculate the area of a parcel.

There are two valdiation checks :

If the parcels weight is less than or equal to 25
If the parcels height is less than or equal to 100

Then the formula to calculate the area is run otherwise error messages are displayed in message boxes.

Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
Dim hcm As Integer
Dim weight As Integer
Dim length As Integer
Dim Cubic As Integer

If txtweight.Text <= 25 Then
hcm = CInt(txthcm.Text)
weight = CInt(txtweight.Text)
length = CInt(txtlength.Text)
Cubic = weight * hcm * length

mcubed.Text = "The Parcel in square metres is " & Cubic.ToString

Else
MessageBox.Show("Please enter a value between 0 and 20kg")
End If
If txthcm.Text <= 100 Then
hcm = CInt(txthcm.Text)
weight = CInt(txtweight.Text)
length = CInt(txtlength.Text)
Cubic = weight * hcm * length
Else
MessageBox.Show("Please enter a value between 1 cm and 100 cm")
End If
End Sub

If there a way to nest these two if then else statements together so that if either condition is not met the error messages are displayed?

Also how to I display the results of the calculation correct to 2 decimal places, this is for my general interest and doesn't relate to this problem.
 
You seem to have some more fundamental problems.

Assuming the "hcm" is intended to be the height, in centimeters, "weight" is intended to be the mass in kg, and "length" is the length also in centimeters.

weight * hcm * length does not give you a cubic anything.

Square meters is not a cubic unit.

I'm not entirely sure what the "area" of a parcel might mean to you, but you have no calculations that produce an area unit.

The reason that you don't see the result to two decimal places is that you are working with integers. Integers don't have decimal parts.
 
sorry width not weight, to calculate cubic feet. That's not important, nesting the statements is the issue.
 
Do your validation with the textbox exit event. Then you will be assured of having only valid input and your calculation can be done in a function.

Code:
Private Sub TextBox1_Exit(ByVal cancel As MSForms.ReturnBoolean)
If CInt(TextBox1.Value) > 10 Then
MsgBox ("must be less than 10")
TextBox1.Value = ""
cancel = True
End If
End Sub
 
How about something like this:

Dim height As Integer
Dim width As Integer
Dim length As Integer
Dim Cubic As Integer

Dim heightValid As Boolean
Dim widthValid As Boolean
Dim lengthValid As Boolean

If IsNumeric(txtwidth.Text) Then
width = CDbl(txtwidth.Text)
If Not (width < 0 Or width > 20) Then
widthValid = True
End If
End If

If IsNumeric(txtheight.Text) Then
height = CDbl(txtheight.Text)
If Not (height < 0 Or height > 20) Then
heightValid = True
End If
End If

If IsNumeric(txtlength.Text) Then
length = CDbl(txtlength.Text)
If Not (length < 0 Or length > 20) Then
lengthValid = True
End If
End If

If Not heightValid Then
MessageBox.Show ("Please enter a value between 0 cm and 20 cm")
Return
End If

If Not widthValid Then
MessageBox.Show ("Please enter a value between 1 cm and 100 cm")
Return
End If

If Not lengthValid Then
MessageBox.Show ("Please enter a value between 0 cm and 20 cm")
Return
End If

Cubic = weight * height * length
mcubed.Text = "The Parcel in square metres is " & Cubic.ToString

 
Thanks for the solutions. Still need help with the decimal places problem.
 
Use a variable type that has decimals then.

Integers do not have decimals, so you have no decimals to show.

You have dimensioned your variables as integers.

You are coercing your text box values into integers.

If you want decimals then use a data type that has decimals.
 
Still need help with the decimal places problem.
mintjulep answered that one in the first reply:
mintjulep said:
The reason that you don't see the result to two decimal places is that you are working with integers. Integers don't have decimal parts.
to fix it you will have to use a type that allows decimals (single, double, currency, etc).
 
I have defined the value as a decimal. I don't understand the syntax to round a caculation to 2dp.
 
Do you want to FORMAT the display to show only two decimals, or do you want to ROUND the value to two decimals?

They are not the same thing.

DisplayAs = Format(SomeNumber, "0.00")
SomeNewNumber = Round(SomeNumber, 2)



 
Thanks. That worked!

Dim twodec As Decimal

twodec = Format(Amount, "0.00")
mcubed.Text = "The Parcel in square metres is " & twodec.ToString
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top