Problems on passing values between modules.

HerrSober

New Member
Joined
Aug 30, 2013
Messages
40
I am building a more complex report and since there are lots of repetive subs I want to structure them into different modules. However I experience empty values for some weird reason. (My question is more detailed written in "Module 3")

In this example I could have solved it by setting Public const x = 2.
But... since both the X and Y value is going to be calculated I find no other solution than to put them on subroutines and calculate it from there.


I am using Microsoft Excel 2010.


Code:
'In Module 3
Sub ExcecutionSub()
ValueSub1
ValueSub2

Debug.Print "The X value goes fine; " & x
Debug.Print [B]"X and Y go together in debug.print:[/B] "; x & y

[B]'''But look when I try it in a msgbox. Only X is visible[/B]

MsgBox x, q

End Sub

Code:
'in Module1
Public y As Long
 
Sub ValueSub1()
y = 2
End Sub

Code:
 'in Module2
Public x As Long

Sub ValueSub2()
    x = 8
End Sub

I hope you can help me out!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Code:
'In Module 3
Sub ExcecutionSub()
ValueSub1
ValueSub2

Debug.Print "The X value goes fine; " & x
Debug.Print [B]"X and Y go together in debug.print:[/B] "; x & y

[B]'''But look when I try it in a msgbox. Only X is visible[/B]

MsgBox x, [B][COLOR=#ff0000]q[/COLOR][/B]

End Sub
The red q should be y shouldn't it?
 
Upvote 0
You aren't using Option Explict.
Rich (BB code):
MsgBox x, q
That line is passing two arguments to MsgBox 1) prompt:= x and 2) Button:= q (=0), which is the value of vbOKOnly.
If you want to see the two values
Rich (BB code):
MsgBox x & ":" & y

Using Option Explicit would have alerted you to the presence of the undeclared typo q
 
Last edited:
Upvote 0
The red q should be y shouldn't it?
You're right! A "typing error". Thanks for noticing.


You aren't using Option Explict.
Rich (BB code):
MsgBox x, q
That line is passing two arguments to MsgBox 1) prompt:= x and 2) Button:= q (=0), which is the value of vbOKOnly.
If you want to see the two values
Rich (BB code):
MsgBox x & ":" & y

Using Option Explicit would have alerted you to the presence of the undeclared typo q

Thanks! It solved my problem :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,222,405
Messages
6,165,864
Members
451,988
Latest member
boo203

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