Consulting

Results 1 to 5 of 5

Thread: Define Worksheet As Public Variable

  1. #1
    VBAX Regular
    Joined
    Oct 2018
    Posts
    43
    Location

    Define Worksheet As Public Variable

    Hi Everyone,

    It may seem trivial but I don't know a lot about Public Sub.

    As I am working on multiple sub at a time I was wondering if I could set up all the variable in one sub.

    Which led me to that code (doesn't work):

    Public ws As Worksheet
    
    Sub Test()
    
    
    Set ws = ActiveWorkbook.Worksheets("Sheet1")
    
    
    End Sub
    
    
    Sub Call_Test()
    
    
    Debug.Print ws.Name
    
    
    End Sub
    When I run it, I have the Run-time error '91'.

    Do you have any ideas of what I am doing wrong here?

    Thanks a lot in advance!
    Edmond

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    If I run Test, then Call_Test it works

    If I just run Call_Test first, or after an error it doesn't since Excel clears things
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    VBAX Regular
    Joined
    Oct 2018
    Posts
    43
    Location
    Indeed, it perfectly works.

    So how would you proceed to let the user decide which Sub he wants to run?

    Public ws As Worksheet, ws2 As Worksheet
    
    Sub Test()
    
    
    Set ws = ActiveWorkbook.Worksheets("Sheet1")
    Set ws2 = ActiveWorkbook.Worksheets("Sheet2")
    Call Call_Test
    
    
    End Sub
    
    
    Sub Call_Test()
    
    
    Debug.Print ws.Name
    
    
    End Sub
    
    
    Sub Call_Test2()
    
    
    Debug.Print ws2.Name
    
    
    End Sub
    Edmond

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by Edmond View Post
    Indeed, it perfectly works.

    So how would you proceed to let the user decide which Sub he wants to run?
    Not nearly enough information, but I'm GUESSING that you mean the user runs Test() and then decides if it Call_Test or Call_Test2???



    I'd architect it like this and let the user run Call_Test or Call_Test2


    Option Explicit
    
    Public ws As Worksheet, ws2 As Worksheet
    
    Sub Call_Test()
        Init
        Debug.Print ws.Name
    
    End Sub
    
    Sub Call_Test2()
        Init
        Debug.Print ws2.Name
    
    End Sub
    
    Private Sub Init()
        Set ws = ActiveWorkbook.Worksheets("Sheet1")
        Set ws2 = ActiveWorkbook.Worksheets("Sheet2")
    End Sub
    
    
    If that's not close, then some more information would be helpful
    ---------------------------------------------------------------------------------------------------------------------

    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

  5. #5
    VBAX Regular
    Joined
    Oct 2018
    Posts
    43
    Location
    Sorry for the lack of information. But I have found what I was looking for with your answer

    Thank you again!
    Edmond

Posting Permissions

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