Consulting

Results 1 to 10 of 10

Thread: Assign worksheet to Global variable. Possible? or Worth it?

  1. #1
    VBAX Newbie
    Joined
    Dec 2022
    Posts
    4
    Location

    Assign worksheet to Global variable. Possible? or Worth it?

    Hello all,
    I'm tying to figure out how to declare a variable of a worksheet as global so that it can be used from any module.
    My best try (failing) looks like this...
    Option Explicit
     
     Public wsI2 As String
     wsI2 = "Input"
     wsI2 = Worksheets(wsI2)
    
     Public wsI As Worksheet
        wsI = Sheets("Input")
    
     Public wsO As Worksheet
        wsO = Sheets("Output")
    
     Public wsR As Worksheet
        wsR = Sheets("Results")
    
     Public HDT As Worksheet
        wsH = Sheets("HDT")
    
     Public HDT As Worksheet
        HDT = Sheets("HDT")
    
     Public GLL As Worksheet
        GLL = Sheets("GLL")
    
     Public GGA As Worksheet
        GGA = Sheets("GGA")
    
     Public HDT As Worksheet
        wsH = Sheets("HDT")
    
    Sub NavPage1()
        wsI2.Select
    End Sub

    I have no idea how to make this work. I've looked everywhere I can think of.
    Last edited by Aussiebear; 10-03-2024 at 01:00 PM. Reason: Added code tags to supplied code

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,800
    Location
    Assuming you only need it from code in the same workbook, just use its codename - e.g. Sheet1
    Be as you wish to seem

  3. #3
    VBAX Newbie
    Joined
    Dec 2022
    Posts
    4
    Location
    Forgive that I am UberNewb but... codename? I know the sheet# remains the same even if you change the name of the tab. Is this what you are referring to?
    Yes only the same workbook. I am looking to do this programmatically via variables. Part is self education, and part application based.

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,800
    Location
    If you look at the worksheet in the Project Explorer in the VB Editor, you will see it has two names. The first one is the code name, and the one in brackets is what appears on the sheet tab. You can simply use the codename as an object in your code (it's like a worksheet variable) - for example:

    msgbox sheet1.name
    Be as you wish to seem

  5. #5
    VBAX Newbie
    Joined
    Dec 2022
    Posts
    4
    Location
    So, I have found this works.. Oddly enough, part of the answer was derived from a completely different question.

    What I would like to know is how I can use these as global variables across all modules.



    Option Explicit
    
    Sub SelectPages()
        Dim WB1 As Workbook
        Dim SH1 As Worksheet
        Dim SH2 As Worksheet
        Set WB1 = ThisWorkbook
        Set SH1 = WB1.Worksheets("Sheet1")
        Set SH2 = WB1.Worksheets("Sheet2")
        SH1.Select
        SH2.Select
    End Sub
    Last edited by Aussiebear; 10-03-2024 at 12:58 PM. Reason: Added code tags to supplied code

  6. #6
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,800
    Location
    You'd need to move these lines:

    Dim WB1 As WorkbookDim SH1 As Worksheet
    Dim SH2 As Worksheet
    to the top of a normal module, outside all procedures, and make the declarations public:

    Public WB1 As Workbook
    Public SH1 As Worksheet
    Public SH2 As Worksheet
    but you'd still need to make sure to run the SelectPages code before any other code. You'd be better off just using ThisWorkbook and the sheet code names directly in your code. They work like public variables and you don't need to initialise them.
    Be as you wish to seem

  7. #7
    VBAX Newbie
    Joined
    Dec 2022
    Posts
    4
    Location
    AHHHH.. got it! Thanks. I am just having trouble wrapping my head around using predefined variables with other modules. Do I need to reference the variables in another module? as below?

    Option Explicit
    
    Dim WB1 As Workbook
    Dim SH1 As Worksheet
    Dim SH2 As Worksheet
    
    Sub SelectPages()
        Set WB1 = ThisWorkbook
        Set SH1 = WB1.Worksheets("Sheet1")
        Set SH2 = WB1.Worksheets("Sheet2")
        SH1.Select
        SH2.Select
    End Sub                         
    
    Sub OtherPages(WB1, SH1, SH2)
        WB1.DoSomething
        SH1.JumpRope
        SH2.EctEctEct
    End Sub

    Or do I even need the "(WB1, SH1, SH2)" on the OtherPages?
    Last edited by Aussiebear; 10-03-2024 at 01:00 PM. Reason: Added code tags to supplied code

  8. #8
    Site Admin VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,495
    Location
    Sorry POE333 but you have missed the point made by Aflatoon.

    Option Explicit
    
    Public WB1 As Workbook
    Public SH1 as WorkSheet
    Public WH2 as Worksheet
    Technically you could also use the word "Global" in place of "Public", but that is simply a matter of individual preference. You should "dim" your variables within the procedure not within the Global statements.

    Sub SelectPages()
        Set WB1 = ThisWorkbook
        Set SH1 = WB1.Worksheets("Sheet1")
        Set SH2 = WB1.Worksheets("Sheet2")
        SH1.Select
        SH2.Select
    End Sub
    Rather than SH1.select or SH2.select use

    With SH1
        'do something
    End With
    do I even need the "(WB1, SH1, SH2)" on the OtherPages?
    No, its un-necessary to do so.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  9. #9
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,800
    Location
    Just:
    Sub OtherPages()
        WB1.DoSomething
        SH1.JumpRope
        SH2.EctEctEct
    End Sub
    As I said though, all of this is unnecessary.
    Be as you wish to seem

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,887
    Location
    1. You just need to understand 'Scope'

    https://learn.microsoft.com/en-us/of...and-visibility

    2. Assigning to an object variable (like Worksheet or Workbook) you need to 'Set' it

    3. I like to use the CodeName for worksheets in VBA for WS that the user might rename since that is not easily changed by the user

    Capture.JPG
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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