Issues with assigning value to variable and passing between subs

cvrband

New Member
Joined
Jan 6, 2016
Messages
47
Office Version
365, 2013
Platform
Windows
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(TextBox1) Then NoCAdd = TextBox1 Else NoCAdd = 0
If IsNumeric(TextBox2) Then NoCTot = TextBox2 Else NoCTot = 0

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

If NoC > 0 then
    Call anothersub (NoC, NoCAdd, NoCTot)
End If
End Sub


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

*more code*

End Sub
 

Some videos you may like

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
Joined
May 7, 2008
Messages
21,758
Office Version
2010
Platform
Windows
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
Joined
Jan 6, 2016
Messages
47
Office Version
365, 2013
Platform
Windows
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
Joined
May 7, 2008
Messages
21,758
Office Version
2010
Platform
Windows
A textbox by definition contains text. IsNumeric tells you whether the text can be converted to a Double.
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,105,930
Messages
5,508,184
Members
408,669
Latest member
AgsikapAko

This Week's Hot Topics

Top