View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jake Marx[_3_] Jake Marx[_3_] is offline
external usenet poster
 
Posts: 860
Default How to declare variable as public.

Hi Mark,

Mark wrote:
Why is this getting an error? It says that the 'public' line is
creating a compiler error -- it's an invalid attribute in the sub.

Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Public Z As Workbook


Z = ActiveWorkbook.Name


To declare a public variable, it must be outside of any procedures.
Example:

Public gbMyVar As Boolean

Sub Test()
gbMyVar = Application.ScreenUpdating
MsgBox gbMyVar
End Sub

If you're just going to use that variable in the current module, you should
use Private instead.

One other item - this code

Z = ActiveWorkbook.Name


will fail because you have declared Z as type Workbook, which is an object
variable. You are trying to set a string (return value of Name property) to
an object variable, which will not work. If you truly want a reference to
the ActiveWorkbook object, you can do this instead:

Set Z = ActiveWorkbook

If you just want the name, then you should declare Z As String.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]