# Issues with assigning value to variable and passing between subs

#### cvrband

##### New Member
I have a form where I gather values from a couple of text boxes. The user can either leave the box blank or enter a whole number typically between 1 and 2000. I'm new to passing values between subs but am trying to learn, so I think this is where my problem may be. As I step through the following code, I am not able to set the value for ‘NoC’ when the following if statement is ‘true’: If NoCAdd + NoCTot = NoCAdd then NoC = NoCAdd. The NoC = NoCAdd line is skipped and when hovering over ‘NoC’ the value is ‘0’ even if NoCAdd has a value of say '3'.

Since the main code is basically setting the value for 'NoC', I am assuming that nothing in the following Subs are affecting this value. Is that corrcet?

Also, I want to hold the value for 'NoC' in the following subs, should I call it ByVal rather than the way I currently show it. (info.: I have more than one following sub, but do not change the value of 'NoC' in them, only use it for other calculations)

What am I doing wrong? Thank you in advance.

Code:
``````Sub FormC()

Dim NoCAdd As Variant, NoCTot As Variant
Dim NoC As Long

If IsNumeric(TextBox2) Then NoCTot = TextBox2 Else NoCTot = 0

If NoCAdd + NoCTot > 0 Then
Else
NoC = 0
End If
End If

If NoC > 0 then
End If
End Sub

Sub anothersub(NoC As Long, ByVal NoCAdd As Long, ByVal NoCTot As Long)

*more code*

End Sub``````

### Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

#### shg

##### MrExcel MVP
NoCCAdd and noCTot are variant/string when assigned to the textbox contents. Declare them instead as Double, or be explicit in type conversion:

Code:
``If IsNumeric(TextBox1) Then NoCAdd = CDbl(TextBox1) Else NoCAdd = 0#``

Last edited:

#### cvrband

##### New Member
Thanks shg! That seems to have solved my issue. I'm teaching myself VBA with the assistance of the internet and the kind people on this board. Can you explain what you meant when you said "be explicit in type conversion"? Is it typical to have to declare the information returned from the textbox or is that only when they are values since you seem to imply that text boxes are by default string-variants? Thanks again for your help.

#### shg

##### MrExcel MVP
A textbox by definition contains text. IsNumeric tells you whether the text can be converted to a Double.