Issues with assigning value to variable and passing between subs

cvrband

Board Regular
Joined
Jan 6, 2016
Messages
63
Office Version
  1. 365
Platform
  1. 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
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
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:
Upvote 0
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.
 
Upvote 0
A textbox by definition contains text. IsNumeric tells you whether the text can be converted to a Double.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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