Do I have to do this...
a...every time if used in a private sub?
b...once if used in public subs?
c...once only per book?
d...every time to be safe?
Do I have to do this...
a...every time if used in a private sub?
b...once if used in public subs?
c...once only per book?
d...every time to be safe?
Re: Dim Variable
You don't necessarily have to do it all...
The main thing to remember is if you dim a variable outside a sub, it will stay in memory after the macro or sub is finished. That can be very handy indeed!
Re: Dim Variable
So it is is loaded as soon as the workbook is? Would it be a good idea, in that case then, to place all dim variables in the workbook object and not in individual modules so they are all pre-loaded?
Thanks for the answer, btw, Aaron
Re: Dim Variable
Quote from max_luxSo it is is loaded as soon as the workbook is? Would it be a good idea, in that case then, to place all dim variables in the workbook object and not in individual modules so they are all pre-loaded?
Thanks for the answer, btw, Aaron
Actually, I'm not really sure at what point the variable is actually instantiated. Honestly, I haven't really cared...
Unless you specifically want to save variables in memory, then there's no reason to dim them outside of the sub that they relate to, and I would not recommend it. I believe that would be unnecessarily taking up space in memory.
Putting the variables in the workbook object... No, I don't think I'd go out of my way to put variables there. I'd put variables that are specific to subs inside the subs, and variables that I want to span multiple subs outside the subs at the top of a standard module. ...and if the variables need to span multiple modules, I might even declare them PUBLIC.
Re: Dim Variable
and to declare them public, one types?
public dim str as string
Re: Dim Variable
See: Excel Variables and Lifetime & Scope of Excel Variables
Don’t have an account yet? Register yourself now and be a part of our community!