Re: Declarations variables, Dim, some guidance please

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



By preference (and it's *mostly* preference), I try to declare only
procedure arguments and local (procedure-level) variables. This is
especially useful in large applications where one may not be sure that a
global/module-level variable isn't changed by an unrelated procedure.

You didn't get a duplicate Dim error because one variable was declared
at the module level, and one was declared within the sub. Local
variables always override global variables within their own procedure.
This is actually an advantage, as one can re-use variable names within
multiple procedures, but each procedure sees only its own, local
variable. For instance, note the reuse of "i" in:

Public Sub Sub1()
Dim i As Long
For i = 1 To 10
Sub2 "Iteration " & i & ": "
Next i
End Sub

Public Sub Sub2(ByVal sStr As String)
Dim i As Long
For i = 1 To 3
Debug.Print sStr & i
Next i
End Sub

If your code is fairly small, and will never be maintained by anyone
else, using module-level variables is probably fine, as long as you keep
track of which procedures are modifying them, and when.

If you're writing code that will be maintained by others, I'd strongly
recommend using local variables as much as possible.



In article <AEBDB225-BCB4-4A42-90DF-EF3175949C17@xxxxxxxxxxxxx>,
Neal Zimm <nealzimm@xxxxxxxxx> wrote:

> In an application that I'm developing I have dim'd quite a few variables in
> Declarations. I'll admit some of it is not wanting to take the time to put
> those vars that are used quite often in many macros within the sub
> SubName(var list) parenthesis.
>
> 1) What advice can you offer on the pro's and cons of this technique? All of
> the application's code is in ONE module.
>
> 2) I got 'bitten' when testing a macro where a var called Draw was dim'd as
> integer in Declarations, had a good value > 0 in prior macros, but was 0 in
> the macro I was testing.
>
> Sure enough, I had dim'd it again, inadvertantly, also as Integer in the
> macro being tested. Hence the 0 value, I guess. The QUESTION is, why did I
> NOT get a duplicate Dim error?
.



Relevant Pages

  • Re: Require input in a cell before saving file
    ... or 2) Digitally sign and verify the macro as safe. ... Cancel As Boolean) ... Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, ... ...and yet I still can save the file with cell E59 being empty. ...
    (microsoft.public.excel.misc)
  • Re: Table in a Form
    ... > Sub AddTimeRow() ... > ' Macro written 12/01/2003 by Charles Kyle Kenyon ... > Dim oTemplate As Template ...
    (microsoft.public.word.tables)
  • Re: How to capture Max cell value (High Water Mark)
    ... a look saving the value and initializing it on worksheet open. ... addressing whatever cell was active on the worksheet. ... Sub Test_Enable_Events ... I'm only using it on the same worksheet where the macro was created. ...
    (microsoft.public.excel)
  • Re: How can my macro run faster ?
    ... I'd keep all those "Sub/End Sub" statements. ... ' SortDossierOrder Macro ... Dim wks As Worksheet ... Dim cLastRow As Long ...
    (microsoft.public.excel.newusers)
  • Re: Insert a Field and assign to button
    ... ' AddTimeRow Macro ... Dim oTemplate As Template ... .CorrectInitialCaps = True ... Sub InsertRowAboveMe() ...
    (microsoft.public.word.vba.general)