+ Reply to Thread
Results 1 to 6 of 6

Variable at Module level

  1. #1
    Forum Contributor
    Join Date
    02-12-2011
    Location
    Hyderabad
    MS-Off Ver
    Excel 2007
    Posts
    263

    Variable at Module level

    Hi All

    I am declaring a two public variables at module level and wish to use SET Statement.

    I am trying to use SET statement at module level / this work book level but i get a error message stating invalid outside procedure

    My Query where can i set the value so that i can use WB1/WS1 across different sheets / procedures

    [/CODE]
    Public wb1 As Workbook
    Public ws1 As Worksheet

    Set wb1 = ThisWorkbook
    Set ws1 = wb1.Sheets("Sheet2")

    [/CODE]
    Never confuse a single defeat to the final defeat.

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Variable at Module level

    Edit:
    I should have my coffee before posting.
    The variable has to be declared at the top of the module and the set statements have to be in a sub or function, as far as I know.
    Last edited by skywriter; 12-02-2022 at 11:04 AM.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Useless variables again ...


    Hi !

    As creating a workbook variable is often useless to just create a worksheet variable !
    Like Set Ws1 = ThisWorkbook.Sheets("Sheet2")

    As obviously ThisWorkbook is already the workbook variable object so not necessary to duplicate it !

    As the sheet is within Thisworkbook object so creating a worksheet variable is useless as already exists its CodeName Sheet2,
    exactly like a worksheet variable …

    As both ThisWorkbook & sheets CodeNames are already Public at project level so already available for any VBA procedure !

  4. #4
    Forum Contributor
    Join Date
    02-12-2011
    Location
    Hyderabad
    MS-Off Ver
    Excel 2007
    Posts
    263

    Re: Useless variables again ...

    Thank you skywriter, marc for your suggestions.

  5. #5
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,097

    Re: Variable at Module level

    Probably the simplest way is to add a function to your module which sets the variables

    Please Login or Register  to view this content.

    Then in your routines you can check them

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Re: Variable at Module level


    Again : useless object variables ! As the simplest way is still to just use ThisWorkbook and sheets CodeNames …

+ 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. Setting module-level object variables
    By chrisanthny in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-26-2013, 08:16 PM
  2. [SOLVED] Declaring Object References at Module Level
    By RGrunden in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-24-2012, 01:47 AM
  3. Declare module level variable using Set statement
    By asha3010 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-24-2010, 05:22 AM
  4. [SOLVED] Application level events - calling from standard module
    By triaz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-24-2006, 04:30 AM
  5. Assign Value to Module Level Variable
    By monir in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 03-22-2005, 11:07 PM
  6. [SOLVED] use of module level variables
    By TxRaistlin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-03-2005, 07:06 PM
  7. HELP!! Module/Procedural level code conflict
    By k483 in forum Excel General
    Replies: 0
    Last Post: 02-17-2005, 11:22 AM
  8. Declaring array constants at module level
    By Microsoft Forum in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-24-2005, 03:06 PM

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