Eswemenasja
Eswemenasja

Reputation: 133

VBA difference between declaring Public var and passing local var to subroutines

The problem is that I've declared all my variables as public and simultaneously I'm passing those to subroutines. Which solution is better and what are the differences? BR Michał

Upvotes: 1

Views: 59

Answers (1)

Chrowno
Chrowno

Reputation: 198

The difference:

  • A public variable can be accessed from every module or routine within the workbook it has been declared in and can even be made accessible to other workbooks
  • A local variable can only be accessed by the module it was declared in

Try to avoid using public variables whereever you can. They might seem like the easy way out but can cause alot of problems the longer the code gets, use up unnecessary memory space and make the code structure quite confusing.

Passing values to a subfunction or -routine is usually the more elegant way and good coding practise.

Upvotes: 1

Related Questions