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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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