Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default How to declare variable as public.

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
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]

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default How to declare variable as public.

Thought this would solve my problem, but it didn't. Any idea why this line,
in a different sub in the same module that I set Z = activeworkbook, doesn't
work....

Range("B2").Select
ActiveCell.FormulaR1C1 = _
"='[" & Z & "]Summary Data'!R503C[32]" 'this line gets error
message

error is 91 - object variable or with block variable not set

"Jake Marx" wrote:

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]


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default How to declare variable as public.

Hi Mark,

Mark wrote:
Thought this would solve my problem, but it didn't. Any idea why
this line, in a different sub in the same module that I set Z =
activeworkbook, doesn't work....

Range("B2").Select
ActiveCell.FormulaR1C1 = _
"='[" & Z & "]Summary Data'!R503C[32]" 'this line gets
error message

error is 91 - object variable or with block variable not set


If you are using Set Z = ActiveWorkbook, then Z holds a reference to the
workbook object. So, in order to get the name, you would have to use Z.Name
instead of just Z.

If that's not the case, please post all the code from that module so we can
help you debug.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declare and Set Public variables jlclyde Excel Discussion (Misc queries) 2 January 28th 09 02:16 PM
PUBLIC DECLARE N10 Excel Programming 0 December 24th 04 06:50 PM
Declare Variable zapatista66[_14_] Excel Programming 0 October 7th 04 05:02 PM
Declare Variable zapatista66[_12_] Excel Programming 2 October 7th 04 04:55 PM
Declare Variable zapatista66[_9_] Excel Programming 1 October 6th 04 08:47 PM


All times are GMT +1. The time now is 09:47 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"