Adding Label Numerical Values correctly

  • Thread starter Thread starter Corey ....
  • Start date Start date
C

Corey ....

I am trying to ADD the values of 3 label Caption IF they are a Numerical
Value.
I have used the LONG approach below, but it only works on the provision that
the user starts at the label63 then to the Label74 and thirdly to the
Label87.
But if the user starts at label74 or Label87, the below code does not add
the values and place the result into Textbox64.

How can i better code this to allow for ALL not matter what Labels are
numerical, and total the values in TextBox64, without having to add every
possible combinatiopn of the current code i have ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If IsNumeric(Label63.Caption) = True And IsNumeric(Label74.Caption) = False
And IsNumeric(Label87.Caption) = False Then TextBox64.Value =
Val(Label63.Caption)
If IsNumeric(Label63.Caption) = True And IsNumeric(Label74.Caption) = True
And IsNumeric(Label87.Caption) = False Then TextBox64.Value =
Val((Label63.Caption)) + Val((Label74.Caption))
If IsNumeric(Label63.Caption) = True And IsNumeric(Label74.Caption) = True
And IsNumeric(Label87.Caption) = True Then TextBox64.Value =
Val((Label63.Caption)) + Val((Label74.Caption)) + Val((Label87.Caption))
TextBox64 = Format(TextBox64, "0.00")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


CtM.
 
TextBox64 = Val(Label63) + Val(Label74) + Val(Label87)
TextBox64 = Format(TextBox64, "0.00")
Done the job.

Thanks Corey.

No worreis mate.

Corey....
:-)
 
Not sure if I have interpreted you question correctly. it appears that you
want to sum the label captions if they are numeric. If this is correct, the
the following should do the trick.

I have used ActiveSheet for the labels. You might need to change this if the
labels are on a Userform.

Sub test()

Dim lngSum As Long

lngSum = 0 'This line possibly not required depending on your other code

With ActiveSheet
If IsNumeric(.Label63.Caption) = True Then
lngSum = lngSum + .Label63.Caption
End If

If IsNumeric(.Label74.Caption) = True Then
lngSum = lngSum + .Label74.Caption
End If

If IsNumeric(.Label87.Caption) = True Then
lngSum = lngSum + .Label87.Caption
End If

.TextBox64 = Format(lngSum, "0.00")
End With

End Sub
 
Back
Top