Public Variable Not Getting Passed

Matt_O

Board Regular
Joined
May 23, 2013
Messages
64
Hi Excel Gurus,

I can't figure out why a public variable will not get passed to another module in the same workbook. I've searched through lots of threads here and across the web and can't figure out if I'm doing something wrong. It's such a basic thing that I've used multiple times and now I can't get it to work. Is it me or Excel that has a bug?

Once the call to the next module's sub is made the Locals window shows no variables. I wish I wasn't stuck on such an apparently simple issue.

First Module:

Code:
Option Explicit
Public x As Long 'count columns
Public y As Long ' count rows


Public Sub getX()


    
    'Gets column count
    Dim x As Long
    x = cn_Sheet1.Cells(1, Columns.Count).End(xlToLeft).Column
    
    'Gets row count
    Dim y As Long
    y = cn_Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
    
    
 'at this point both x and y variables contain the accurate counts


    Call test




    
End Sub

The second module with sub 'test'

Code:
Option Explicit


Public Sub test()


    Debug.Print "Row count is " & y

' results in zero as no variable was passed to this module.


End Sub

Thanks,
Matt
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Matt

Why have you declared x and y twice, once as public and again within the getX sub?
 
Upvote 0
Hi Norie,

Good question with a no good answer. Pure oversight.

I never even noticed the 2 Dims once I made them public. Just constantly glossed over that small piece of code!

Thanks for super quick reply and fix. Now we know which entity is buggy.

Matt
 
Upvote 0

Forum statistics

Threads
1,221,525
Messages
6,160,329
Members
451,637
Latest member
hvp2262

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