How to quickly identify unused variables?

As I reuse code, I often end up with variables that are not used in my new routines. Identifying remnant variables is a tedious process which is compounded by SolidWork’s tendency to frequently crash as I comment out variables I think are unused and then recompile. Do you have any tricks to clean up code like this?

Thank You

For vba macros, I comment out most of them (except the few which I can easily spot) and add Option Explicit at the top. Now when running the macro, SW will prompt for the undefined variables, and I remove comment from them. And keep on doing it until all done. There are some tools available, but I have never tried them.

When using VB.net, the system itself prompt for not required variables.