Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default how to declare a public workbook?

I'd like to declare a workbook and a worksheet before the first
procedure so that all other procedures can use them.

When I tryied to use Set Statement as "Set myBook =
Workbooks("test.xls")", I've got "invalid outside of a procedure"
error.

So, I guessed the Set statement may not work outside of a procedure
(I'm not sure). And then, I declared a string constant as "Public
Const myBook as "test.xls"" and used myBook in other procedure as
"Workbooks(myBook)", which worked fine.

However, I'd like to use something like myBook.mySheet.myRange.---,
insted of
Workbooks("myBook").Worksheets("mySheet").Range("m yRange").---,

Any recommendation?

Thanks in advance.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default how to declare a public workbook?

I'm quite positive you cannot set any type of object outside of a sub/function

Here is something that offers a few different ways of setting either
WorkBooks, WorkSheets or a Range

Public Sub test()

Dim aBook As Workbook
Dim aSheet As Worksheet
Dim aRange As Range


Set aBook = Workbooks("Book1")
Set aSheet = Workbooks("book1").Sheets("Sheet1")
Set aRange = Workbooks("Book1").Sheets("Sheet1").Range("A1:A10" )
aBook.Sheets("Sheet1").Cells("1, 5") = "Funkah"
aSheet.Cells(1,3).Value = "Rar"
aRange.value = "Test"

End Sub

It's all pretty much straight forward, but makes it all that easier to
shrink down certain calls.

Hope this helps somewhat! :)

Nate

"choi4u" wrote:

I'd like to declare a workbook and a worksheet before the first
procedure so that all other procedures can use them.

When I tryied to use Set Statement as "Set myBook =
Workbooks("test.xls")", I've got "invalid outside of a procedure"
error.

So, I guessed the Set statement may not work outside of a procedure
(I'm not sure). And then, I declared a string constant as "Public
Const myBook as "test.xls"" and used myBook in other procedure as
"Workbooks(myBook)", which worked fine.

However, I'd like to use something like myBook.mySheet.myRange.---,
insted of
Workbooks("myBook").Worksheets("mySheet").Range("m yRange").---,

Any recommendation?

Thanks in advance.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default how to declare a public workbook?

You can declare the workbook variable outside of the procedure, but you have
to load it from within a procedure.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"choi4u" wrote in message
...
I'd like to declare a workbook and a worksheet before the first
procedure so that all other procedures can use them.

When I tryied to use Set Statement as "Set myBook =
Workbooks("test.xls")", I've got "invalid outside of a procedure"
error.

So, I guessed the Set statement may not work outside of a procedure
(I'm not sure). And then, I declared a string constant as "Public
Const myBook as "test.xls"" and used myBook in other procedure as
"Workbooks(myBook)", which worked fine.

However, I'd like to use something like myBook.mySheet.myRange.---,
insted of
Workbooks("myBook").Worksheets("mySheet").Range("m yRange").---,

Any recommendation?

Thanks in advance.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default how to declare a public workbook?

You can put the code in the Workbook_Open procedure in the ThisWorkbook code
module (it must be in ThisWorkbook, not some other module).

Private Sub Workbook_Open()
On Error Resume Next
Set myBook = Workbooks("Test.xls")
If Err.Number < 0 Then
' Test.xls not found. do something
Else
' Test.xls found. do something.
End If
End Sub

Next, declare the myBook variable in a regular code module (not in
ThisWorkbook).

Public myBook As Excel.Workbook


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



"choi4u" wrote in message
...
I'd like to declare a workbook and a worksheet before the first
procedure so that all other procedures can use them.

When I tryied to use Set Statement as "Set myBook =
Workbooks("test.xls")", I've got "invalid outside of a procedure"
error.

So, I guessed the Set statement may not work outside of a procedure
(I'm not sure). And then, I declared a string constant as "Public
Const myBook as "test.xls"" and used myBook in other procedure as
"Workbooks(myBook)", which worked fine.

However, I'd like to use something like myBook.mySheet.myRange.---,
insted of
Workbooks("myBook").Worksheets("mySheet").Range("m yRange").---,

Any recommendation?

Thanks in advance.


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
Declare a public variabl Patrick Simonds Excel Programming 6 March 31st 06 01:53 AM
Public declare for Set.... zapatista66[_17_] Excel Programming 2 September 30th 05 01:29 PM
How to declare variable as public. Mark Excel Programming 3 April 7th 05 06:27 PM
PUBLIC DECLARE N10 Excel Programming 0 December 24th 04 06:50 PM


All times are GMT +1. The time now is 06:38 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"