VBA - variable declaration

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Hi,

I have a few macros that are being edited.
My question - is there a way to tell if a declared variable
has been used in the "sub Marcro()" or if it was just declared (i.e. dim
Variable3 as double) and not used.

This would be useful because it would allow me to more easily delete unused
variables and eliminate clutter.

Thanks
 
Since it's only a few macros, the easiest way would probably be to use
Find() with the variable name in the VBE, which has an option to look in
the current procedure.

If you require variable declaration (which you absolutely should) by
using Option Explicit, you could also comment out the declaration of
suspect variables, then compile - if the variables are used, it will
generate a compile error, if not, you're free to delete the declaration.
 
If the first line in the module is not OPTION EXPLICIT, enter it so that all
variables have to be declared before using them and do the following:

Place an apostrophe at the beginning of the DIM statement to make the
declaration a comment

Recompile the code by clicking DEBUG and selecting COMPILE VBA PROJECT.

If it compiles OK the variable was not used.
 
Download MZ-Tools, it has a facility to analyze a project and it tells you
that. It is free.

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top