VBA Etiquette: Dim all variables at the top of Sub or Dim as you Go?

Bill Bisco

Active Member
Joined
Aug 8, 2007
Messages
446
When I was taught VBA, my instructor stated that we should always Dim all our variables at the top of the code. However, I see several examples of people "Dimming" variables as they go?

What is this board's opinion of the best practice of where and when to Dim Variables?

Thanks,

Bill
 
And it comes with the added bonus of the fact that if you find yourself scrolling back and forth over a million lines to check your variable names, your code is too long, and it forces you to break it up into smaller functions and subroutines.

While I definitely vote for smaller routines, I found out a few months ago (thanks to Zack the Table King :)), that if you select a variable and press Ctrl+I, it'll bring up the variable type! I used to split my window so I could keep the variables at the top, but then he showed me that trick.
 
Last edited:
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
that if you select a variable and press Ctrl+I, it'll bring up the variable type!

Thanks, this can be quite useful and is something i was not aware of as im sure plenty of others weren't either.

I generally have the variable type in the name but sometimes i can forget e.g. bVariable1 for Boolean and sVariable2 for String
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,105
Messages
6,128,859
Members
449,472
Latest member
ebc9

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top