Results 1 to 4 of 4

Thread: EXCEL: How To: Pass Workbook Handle to Sub [RESOLVED - duh]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Resolved EXCEL: How To: Pass Workbook Handle to Sub [RESOLVED - duh]

    Esteemed Forum Participants and Lurkers:
    ===============================
    Excel 2003 VBA

    I must be doing something dumb here ... I can't seem to pass a workbook reference to a Subroutine ...
    Code:
    Sub aTest()
        Dim aBook As Workbook
        
        Set aBook = ActiveWorkbook  'Set a handle to a WorkBook
        Debug.Print aBook.Name      'Prove that we can access by handle
        Pass_Book (aBook)           '<< THIS FAILS
    End Sub
    
    Sub Pass_Book(TestBook As Workbook)
        Debug.Print TestBook.Name, TestBook.Sheets(1).Name
    End Sub
    I get an error when I call "Pass_Book":

    Run-time error '438': Object doesn't support this property or method

    Thank you for any and all comments, suggestions, and assistance.
    Last edited by Webtest; Oct 26th, 2005 at 03:13 PM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: EXCEL: How To: Pass Workbook Handle to Sub ???

    Take the parenthesis off
    VB Code:
    1. Sub aTest()
    2.     Dim aBook As Workbook
    3.    
    4.     Set aBook = ActiveWorkbook  'Set a reference to a WorkBook
    5.     Debug.Print aBook.Name      'Prove that we can access by reference
    6.     Pass_Book aBook           '<< THIS WORKS NOW
    7. End Sub
    8.  
    9. Sub Pass_Book(TestBook As Workbook)
    10.     Debug.Print TestBook.Name, TestBook.Sheets(1).Name
    11. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: EXCEL: How To: Pass Workbook Handle to Sub ???

    Why in the crappy blazes do the "Hint" popups ALWAYS show parentheses whether they should be there or not? I'll never learn!

    Thanks RobDog.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  4. #4
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: EXCEL: How To: Pass Workbook Handle to Sub [RESOLVED - duh]

    You only need the parenthesis when you are calling a sub in this method..

    VB Code:
    1. Call Pass_Book(aBook)

    This is old style code, and I very much doubt that people still use it, it is however required if you are interested in a return value of a function, for example to return the button that was pressed on a MsgBox Function, or another way of looking at this is to say..

    VB Code:
    1. varName = FunctionName(Parameters)

    If you weren't interested in the returned value you would just call the function like so..

    VB Code:
    1. FunctionName Parameters

    But this type of function call is a waste of memory, and if you were to use nothing but non-return calls to functions throughout your code then you would be better off setting these as Sub procedures instead, but that's another argument I don't want to kick off on this forum..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

Posting Permissions

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



Click Here to Expand Forum to Full Width