Global variable declaration from class module

calendar_today Asked Feb 5, 2014
thumb_up 9 upvotes
history Updated April 16, 2026

Question posted 2014 ยท +4 upvotes

I have a workbook with multiple modules and multiple subs. There are some variables though that are usesd constantly in most subs such as given worksheest.

eg

dim cr as worksheet
set cr=sheets("combined_report")

I have this written in way too many subs. Can I write this once in say a class module and use “cr” from any sub in any module without having to reassign it?

Accepted answer +9 upvotes

You can do this with a function in a standard module and cache the reference using the Static keyword:

Function CR() As Worksheet
    Static CRSheet As Worksheet
    If CRSheet Is Nothing Then Set CRSheet = Sheets("combined_report")
    Set CR = CRSheet
End Function

Top excel-vba Q&A (6)

+9 upvotes ranks this answer #73 out of 136 excel-vba solutions on this site .