+ Reply to Thread
Results 1 to 14 of 14

create module by means of VBA

  1. #1
    Registered User
    Join Date
    07-07-2014
    Location
    the Netherlands
    MS-Off Ver
    2016
    Posts
    56

    create module by means of VBA

    I want to create a new module in a subroutine. I use excel 2016. In an other subroutine I want to put a string in the new module. The sting will contain a lot of functions and subroutines.

    Who can help?

    Kind regards

    Jan (John)

  2. #2
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: create module by means of VBA

    In order to do this, you have to;

    1) File>> Options>> Trust Center >>>> Click "Trust Center Settings" button. Then, check the option "Trust access to the VBA project object modal"

    2) In the VBA editor, Tools >> References find and add the reference "Microsoft Visual Basic for Applications Extensibility 5.3"

    Now, copy and paste the following code to a new module.

    The following procedure will add a new module named "NewModule" to your project

    Please Login or Register  to view this content.

    The following code will add a procedure to "NewModule" which is created by the above code...

    Please Login or Register  to view this content.

    If you want to add procedure or functions that is allready written in a Text file and import them into this "NewModule", then you can use this;

    Please Login or Register  to view this content.

    If you wish to list all the modules available in your project with their types, this is the code that you can use;

    Please Login or Register  to view this content.

    If you wish to delete the module "NewModule" in case of you don't need any more, then you can use the below code;

    Please Login or Register  to view this content.
    Last edited by Haluk; 02-19-2019 at 08:54 AM.

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: create module by means of VBA

    Please Login or Register  to view this content.
    You forgot to include the function.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Registered User
    Join Date
    07-07-2014
    Location
    the Netherlands
    MS-Off Ver
    2016
    Posts
    56

    Re: create module by means of VBA

    Dear Haluk,

    Thanks a lot. I'm very happy with all your subroutines!!! Just what I needed, especially the "Trust access to the VBA project object modal" seems to be essential.

    I have one problem. The listing of the modules gives an error on "CompTypeToName(VBComp)" (unknown function or subroutine).

    And I have two questions:
    1) How to use a particular cell in a worksheet as text file for use in the module?
    2) How to close all modules?

    Kind regards

    Jan (John)

  5. #5
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: create module by means of VBA

    Quote Originally Posted by bakerman2 View Post
    You forgot to include the function.
    Quote Originally Posted by Jan Lichtenbelt View Post

    I have one problem. The listing of the modules gives an error on "CompTypeToName(VBComp)" (unknown function or subroutine).
    ...
    ..
    The function, named "CompTypeToName" has been added in post #2

    Quote Originally Posted by Jan Lichtenbelt View Post
    And I have two questions:
    1) How to use a particular cell in a worksheet as text file for use in the module?
    2) How to close all modules?
    I don't know what your particular cell houses, so this will be just a guess......

    I assume that, your mentioned "particular cell" is A1 on the active sheet and it is housing a text .... such as;

    Please Login or Register  to view this content.
    Then, you can add this text as a procedure into module "NewModule" with the code below;

    Please Login or Register  to view this content.
    And, as per your question #2 .... I didn't understand what you mean by "closing the modules".

    .
    Last edited by Haluk; 02-19-2019 at 08:39 AM.

  6. #6
    Registered User
    Join Date
    07-07-2014
    Location
    the Netherlands
    MS-Off Ver
    2016
    Posts
    56

    Re: create module by means of VBA

    I like the Excel equivalent of the access subroutine:
    Sub CloseModules()
    'Closes all libraries
    '14 september 2006, Jan Lichtenbelt
    Dim dbs As Object
    Dim obj As AccessObject

    Set dbs = Application.CurrentProject
    ' Search for open AccessObject objects in AllModules collection.
    On Error Resume Next
    For Each obj In dbs.AllModules
    DoCmd.Close acModule, obj.Name, acSaveYes
    Next obj

    Set obj = Nothing
    Set dbs = Nothing

    End Sub

  7. #7
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: create module by means of VBA

    I don't have MS Access so .... I don't know what is being done with the code in your message #6

    Do you want to close the VBA window itself and see only the Excel window?

    Maybe its better if you can provide 2 pictures related with "Closing Modules"...... before and after.
    Last edited by Haluk; 02-19-2019 at 10:24 AM.

  8. #8
    Registered User
    Join Date
    07-07-2014
    Location
    the Netherlands
    MS-Off Ver
    2016
    Posts
    56

    Re: create module by means of VBA

    I'm working with the Dutch version of Excel 2016.
    That means that it is sometimes hard to translate things into the english language.
    But what I want mean is that a subroutine closes all module windows, like can be seen in the pictures.

    (left side: all windows closed, right side: all module windows open)
    Attached Images Attached Images

  9. #9
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: create module by means of VBA

    Hi again;

    You can try the code below but, note that; "Trust access to the VBA project object modal" option should be active before you run the code.

    Please Login or Register  to view this content.
    .

  10. #10
    Registered User
    Join Date
    07-07-2014
    Location
    the Netherlands
    MS-Off Ver
    2016
    Posts
    56

    Re: create module by means of VBA

    I changed it into:
    Sub Test_Close_CodePane()
    On Error GoTo Fout
    Application.VBE.ActiveCodePane.Window.Close
    Exit Sub
    Fout:
    MsgBox Err.Number & " " & Err.Description

    End Sub
    But no error message. Do this subroutine step-by-step, nothing happened. But just run this subroutine, ONLY the Module window with this subroutine closed, all other module windows stayed open.

  11. #11
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: create module by means of VBA

    Yes, that code closes the active code pane only....

    So; if you want to close all of them, then use this;

    Please Login or Register  to view this content.
    .
    Last edited by Haluk; 02-19-2019 at 12:16 PM.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: create module by means of VBA

    Jan

    What are you trying to do?
    If posting code please use code tags, see here.

  13. #13
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: create module by means of VBA

    @haluk

    what command would also close the userform window, i looked though the codes and couldn't find it.

  14. #14
    Registered User
    Join Date
    07-07-2014
    Location
    the Netherlands
    MS-Off Ver
    2016
    Posts
    56

    Re: create module by means of VBA

    Dear Haluk

    All works now, as I wanted. Again, great thanks for help. Your solutions are simple, your answers very quick and you listen very good to my questions. That gives this Forum a high level.

    Kind regards

    Jan

+ 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. comparing means by means of Least Significant Differences (LSD) method
    By rmrf in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-07-2013, 12:14 PM
  2. Create new workbook, module and code using vba
    By pgm in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-12-2012, 06:03 PM
  3. Create A Macro Within VBA Module
    By angushiro in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-24-2011, 10:17 AM
  4. how to access Sheet module, normal module, Worbook module to type code
    By alibaba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2009, 07:51 AM
  5. create event in class module
    By x taol in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-26-2006, 10:40 PM
  6. [SOLVED] How to create module wide 'constant'?
    By Alex in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2006, 12:30 PM
  7. Replies: 0
    Last Post: 01-11-2006, 02:35 PM
  8. Create an add-in from a VB module
    By Wayne_Perth_WA in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-02-2005, 05:05 AM

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