workbook open event not storing global variables

zack_

Board Regular
Joined
Apr 18, 2014
Messages
79
Hey All,

I have the below code:

Code:
Option Explicit
Public lr As Long


Private Sub Workbook_Open()
Let lr = Range("A" & Rows.Count).End(xlUp).Row
End Sub

my understanding of global variables is that they are saved and available to all modules within the workbook. When I try to test this variable to see if it stored the correct value with a msgbox in a new worksheet, the message box returns a blank value.

does anyone know why the workbook open event would not be storing this variable?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Public variables should be declared in standard modules (Insert>Module)?
 
Upvote 0
That definition of LR will find the last row of whatever worksheet is active when the workbook is open.
If the user adds rows to that worksheet, the value will not reflect the added data, nor will it be applicable to any other worksheet in the workbook.

It would make more sense (i.e. set values in the objects that they are part of) to put this in every sheet's code module (not the ThisWorkbook)

Code:
Property Get LR() as Long
    LR = Me.Range("A" * Rows.Count).End(xlUp).Row
End Property


Then one could refer to the Last row of any worksheet with code like this.

Code:
MsgBox Worksheets("Sheet2").LR & " is the last row in " & Worksheets("Sheet2").Name

And it would reflect any additions made by the user.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,377
Members
448,888
Latest member
Arle8907

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