View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Gareth Roberts Gareth Roberts is offline
external usenet poster
 
Posts: 17
Default Assign Value to Module Level Variable

Depending on what you're doing with myOptFile you might want to declare it
as a string

Const myOptFile as string = "Test.xls"

Otherwise it's still technically a variant (as I understand) it and that
occasionally throws up type mismatch problems.

HTH,
Gareth

"monir" wrote in message
...
There are 8 VBA regular macros (macro1 to macro8) in a single module.

Each
macro includes the following 3 statements:
.............................................
....Dim myOptFile As String 'statement # 1
....myOptFile = "Test.xls" 'statement # 2
....Call myMacro (myOptFile) 'statement # 3
............................................

All macros work fine.

Now, for easy maintenance, I moved the procedure declaration statement # 1
to the module Declarations section at the top, and deleted same from the 8
macros.
Then I left the assignment statement # 2 in one macro (macro1) and deleted
same from the other macros.

When I tried to run macro3(), I got "Run-time error '13': type mismatch",
with reference to myOptFile in the call statement.

Isn't that how one makes a variable visible to all procedures in a module

?
Declare the variable at the top, and assign it in any single procedure.

Thank you.