Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 6th, 2005, 11:46 AM
Authorized User
 
Join Date: Feb 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can an object reference be public?

Hi all,

Look at the following example:

Public Sub set_wrb
Set wrb = ThisWorkbook.Sheets("Sheet1")
End sub

As far as I can see, wrb is only known within the module where it is declared. Even if I declare wrb Public, the object is not known outside the routine set_wrb (Runtime error 424: Object Required).

How can I access wrb outside set_wrb without repeating the Set statement in every routine?

Thanks in advance

-Max
 
Old March 6th, 2005, 11:53 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Declare it as a public function, or property

eg.

Code:
Public Property Get Wrb() As Excel.Worksheet
  Set Wrb = ThisWorkbook.Sheets("Sheet1")
End Property
or

Code:
Public Function Wrb() As Excel.Worksheet
  Set Wrb = ThisWorkbook.Sheets("Sheet1")
End Function
 
Old March 6th, 2005, 01:22 PM
Authorized User
 
Join Date: Feb 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default

this works excellent.

thank you





Similar Threads
Thread Thread Starter Forum Replies Last Post
object reference not set to instant of an object shahidrasul ASP.NET 2.0 Basics 1 September 5th, 2008 02:01 PM
Object Reference not set to an instance of object srinivas_chakka ASP.NET 1.0 and 1.1 Professional 0 February 8th, 2006 11:56 AM
How to reference Public Variable in Code Gino Liu Access VBA 2 June 18th, 2003 01:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.