+ Reply to Thread
Results 1 to 8 of 8

referencing to an object in another subroutine

  1. #1
    Registered User
    Join Date
    01-24-2009
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2003, 2007
    Posts
    61

    referencing to an object in another subroutine

    Hi guys,

    Is it possible to refer to an object which is in another subroutine?

    Or should i have to give its definition in each sub i need it?
    Last edited by navwelch; 04-03-2014 at 05:15 AM.

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: referencing to an object in another subroutine

    you can pass the object as an argument from one routine to the other or you can declare the variable outside both procedures-either as Private if both routines are in the same module or as Public if they're not
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    01-24-2009
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2003, 2007
    Posts
    61

    Re: referencing to an object in another subroutine

    i am sorry, but i didnt fully get what you were saying. I am starting to learn the technical names so please do excuse my questions:

    so i declared the variable as:
    Private monthdatadate As Range
    monthdatadate = Sheets("report").Range(Cells(2, 1), Cells(2, 4 + 2))

    then when i tested a sub it says invalid outside procedure

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: referencing to an object in another subroutine

    you can only put the declaration outside the procedures
    Please Login or Register  to view this content.
    the assignment has to be within a procedure and you've gotta use Set because it's an object variable (and properly qualify the cells properties)
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-24-2009
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2003, 2007
    Posts
    61

    Re: referencing to an object in another subroutine

    oh i get it,

    thanks a lot Joseph!

  6. #6
    Registered User
    Join Date
    01-24-2009
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2003, 2007
    Posts
    61

    Re: referencing to an object in another subroutine

    the test sub runs only wen the original sub has already been run. so i must create a new sub only for declarations. then ensure that it is run whenever i open the excel file so that any sub can be run without the need to run the original sub. i tried to save the declarations sub in Private Sub Workbook_Open(). but it wont work, do you have any solution?

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: referencing to an object in another subroutine

    normal module
    Please Login or Register  to view this content.
    thisworkbook module
    Please Login or Register  to view this content.
    or create a public function that returns this range

  8. #8
    Registered User
    Join Date
    01-24-2009
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2003, 2007
    Posts
    61

    Re: referencing to an object in another subroutine

    ok! 'public' has to be used since it goes outside the module, i get it now! phew!

    thanks a lot!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Referencing to chart object
    By Joaoack in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 04-26-2013, 11:29 AM
  2. Passing Object Array to a Subroutine
    By Farmer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-21-2011, 03:09 PM
  3. [SOLVED] Subroutine calls between workbooks and object references
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-15-2006, 11:40 AM
  4. Referencing Object Names with Variable
    By jbl25 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-25-2005, 08:05 PM
  5. [SOLVED] Referencing a file name within a called subroutine
    By Matt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-24-2005, 04:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1