Results 1 to 15 of 15

Thread: Excel and cross-workbook code

  1. #1

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Excel and cross-workbook code

    I have an excel project that has modules and class modules which need to be in almost every workbook. It's a hassle to have to make sure every workbook has the most up to date code, and I was just wondering if there was some way to make code available to the entire excel application? DLLs maybe? (which I would go about making, how?)
    Don't pay attention to this signature, it's contradictory.

  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 and cross-workbook code

    I see three ways to do it.

    1. Create an Excel Add-In.
    2. Create a dll.
    3. Use code to modify the macros in all workbooks


    I havent done it myself for VBA but I would assume that if you create a VB Dll to do it
    it would be the same. otherwise you would need the Developer version of
    Office which can compile.

    I think it may be easiest to use my CodeBank code to modify VBA code from VB (#3)!
    You can check it out here.

    HTH
    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
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: Excel and cross-workbook code

    Quote Originally Posted by RobDog888
    I see three ways to do it.

    1. Create an Excel Add-In.
    2. Create a dll.
    3. Use code to modify the macros in all workbooks


    I havent done it myself for VBA but I would assume that if you create a VB Dll to do it
    it would be the same. otherwise you would need the Developer version of
    Office which can compile.

    I think it may be easiest to use my CodeBank code to modify VBA code from VB (#3)!
    You can check it out here.

    HTH
    All of the code is protected so code modifying code won't work. Apparently the user needs to add the addin, and this is supposed to have as little to do with excel (according to the user) as possible.

    How would I go about the DLL? I've never used a non-windows API one before.
    Don't pay attention to this signature, it's contradictory.

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

    Re: Excel and cross-workbook code

    I am not 100% sure, but I believe you can just create the dll in VB. Start VB
    and select ActiveX Dll as the project. Then add your code and compile. In
    the vba project properties of an Excel workbook, you can add the reference
    to the dll. Then the wb should act like the code is all behind the wb.

    I am only not sure of this because they do have a Developer version for
    Office XP only and they havent put anything out for any other version. SoI
    think it should work. Although if for 2003, I know they have VSTO which does
    create dlls but is used with .NET

    HTH
    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

  5. #5

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: Excel and cross-workbook code

    Quote Originally Posted by RobDog888
    I am not 100% sure, but I believe you can just create the dll in VB. Start VB
    and select ActiveX Dll as the project. Then add your code and compile. In
    the vba project properties of an Excel workbook, you can add the reference
    to the dll. Then the wb should act like the code is all behind the wb.

    I am only not sure of this because they do have a Developer version for
    Office XP only and they havent put anything out for any other version. SoI
    think it should work. Although if for 2003, I know they have VSTO which does
    create dlls but is used with .NET

    HTH
    Alright, so I go in vb and make a dll then:
    Write the code:
    public function test as single
    test = rnd*50
    end function

    Compile
    Add it as a reference
    Then in excel I write
    msgbox test
    ?
    Don't pay attention to this signature, it's contradictory.

  6. #6

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: Excel and cross-workbook code

    nevermind, i got it. I actually have to create an instance of the class...

    Wow, that's cool!

    Now, can I use code with excel commands (worksheet) in these dlls somehow?
    Don't pay attention to this signature, it's contradictory.

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

    Re: Excel and cross-workbook code

    Yes, add a reference to Excel but it will be removed before you compile. Then
    program in the reference object you need. Then when done change your
    Excel Object declarations to late bound and remove the reference. This will
    make it easier to program and also allow you to use the dll accross multiple
    versions of Excel.

    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

  8. #8

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: Excel and cross-workbook code

    For example, I want to do this:

    application.enableevents = false

    But i get a compile error (duh) because application isn't defined. Is there anyway to get around this?
    Don't pay attention to this signature, it's contradictory.

  9. #9

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: Excel and cross-workbook code

    Alright, next:

    How do I get at a public function inside a module in the dll? Classes is easy, but I can't seem to access the modules.
    Don't pay attention to this signature, it's contradictory.

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

    Re: Excel and cross-workbook code

    In the VBA Editor with the reference to your dll, use the Object Browser to see
    if you can see your function in the module. If its declared as Public then it
    should be visible in the OB.
    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

  11. #11

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: Excel and cross-workbook code

    Quote Originally Posted by RobDog888
    In the VBA Editor with the reference to your dll, use the Object Browser to see
    if you can see your function in the module. If its declared as Public then it
    should be visible in the OB.
    They don't show up, and they are all declared public.
    Don't pay attention to this signature, it's contradictory.

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

    Re: Excel and cross-workbook code

    I'm not 100% fluent at dlls, but it should be visible. What if you placed it in the class?
    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

  13. #13

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: Excel and cross-workbook code

    Quote Originally Posted by RobDog888
    I'm not 100% fluent at dlls, but it should be visible. What if you placed it in the class?
    I tried placing it in the class, but I would have to rewrite all the functions calls to include the "class.", and the classes called by the dll would be different instances than the ones I called (inside one class another is called, so it must be declared with, etc.) I just decided that this type fo thing didn't belong inside a DLL.
    Don't pay attention to this signature, it's contradictory.

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

    Re: Excel and cross-workbook code

    If a dll is no longer considered the way to go then what about the Add-In?
    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

  15. #15

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: Excel and cross-workbook code

    Quote Originally Posted by RobDog888
    If a dll is no longer considered the way to go then what about the Add-In?
    I just realized I can include a form in the dll, and use control arrays in excel!

    I'm going to stay the path of 'copy-paste' for now.
    Don't pay attention to this signature, it's contradictory.

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