+ Reply to Thread
Results 1 to 13 of 13

Delete code module and import same name

  1. #1
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Harrisburg, PA
    MS-Off Ver
    Office 365 MSO (16.0.10730.20264) 32-bit
    Posts
    160

    Delete code module and import same name

    Hi All..
    I have a file used throughout the world, with code modules. When an upgrade is needed, I want to remove a module then import a new one (well actually like qty 7). I have the new modules on a sharepoint site and mapped drive to them.

    This is what seems to happen... In the code below, I delete the module, then import the same module name while getting no errors. But, the imported module has a new name, but with a "1" at the end - it seems that in the Project Explorer window, even though the old module was deleted, it still shows. So, the new imported module gets the same name with the 1 after it. If I break the code after deleting the module, save and exit, then re-open, the original module is actually gone. Then when I import the new one, it imports with the correct name. Seems like the project explorer window need to be "Refreshed" after the delete to see that the module was deleted so I can import with the correct (unused) name.

    Any way to refresh / rebuild the vb components so the newly imported module is imported with the correct name?

    Please Login or Register  to view this content.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Delete code module and import same name

    Hi there,

    For what it's worth, the following code seems to work correctly on my system (Excel 2016):

    Please Login or Register  to view this content.
    The highlighted values may be altered to suit your own requirements.


    Hope this helps.

    Regards,

    Greg M

  3. #3
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Harrisburg, PA
    MS-Off Ver
    Office 365 MSO (16.0.10730.20264) 32-bit
    Posts
    160

    Re: Delete code module and import same name

    Thanks for the reply... but that code is the same as mine with the same result. I tried it on a 32bit system and also on my RDC which is 64bit. I guess I can add to the workbook open a check for module names, and if any end in 1, then I could rename? Can I rename a module?

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Delete code module and import same name

    Hi again,

    Thanks for that feedback.

    As I can't replicate your problem I obviously can't test my suggestions, but the following approach might be worth trying:

    run a routine which deletes the old VBA CodeModules;
    run a routine which uses the OnTime method to immediately (or after e.g. one second) run a routine which imports the new VBA CodeModules;
    allow the importing routine to run.

    I have often found that using the OnTime method in this way allows Excel to "catch its breath" between operations and achieve the required results.

    Hope this helps.

    Regards,

    Greg M

  5. #5
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Harrisburg, PA
    MS-Off Ver
    Office 365 MSO (16.0.10730.20264) 32-bit
    Posts
    160

    Re: Delete code module and import same name

    This was strange. I could delete the modules thru the immediate window, no problem. But in the udf it would not do it, as far as I saw, without closing the file. I tried to rename each module before import (that worked) and the import worked. But, after the import, I still could not delete the renamed modules. SO, my workaround is to edit every module... Delete all lines, then read the .bas and insert each line, in each module:

    Please Login or Register  to view this content.

  6. #6
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Delete code module and import same name

    Hi again,

    Many thanks for that very useful follow-up information.

    Regards,

    Greg M

  7. #7
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Harrisburg, PA
    MS-Off Ver
    Office 365 MSO (16.0.10730.20264) 32-bit
    Posts
    160

    Re: Delete code module and import same name

    Further followup.... Please comment on this... I read somewhere something about if a variable is set to something, the something is deleted and the variable still exists, bla bla bla. Not sure about this, but my solution works regardless of the created VBComp variable.

    set the vairable to the module
    rename the module
    delete the renamed module
    import the new module with the same name as the original module. it Works!
    Please Login or Register  to view this content.
    BOOM!

  8. #8
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,224

    Re: Delete code module and import same name

    The method using OnTime suggested by Greg M should work. It may be that you have implemented his idea incorrectly.
    I will explain how Excel behaves when you change the code in a module or replace modules.
    You should know that after interfering with the VBA project via code, when the main procedure (and also the thread) runs out, the VBA project is recompiled.
    When in a single thread (no matter how many procedures called consecutively) you first delete a module and then import a new one) the recompilation of the project will occur only after the main procedure ends. Assuming that we want to replace MyModule, during the operation of the macro the machine "remembers" that the project contains MyModule. Importing another module with the name MyModule will result in MyModule1 (because the name MyModule is "occupied" all the time), even though MyModule was removed a moment ago. As I mentioned earlier, only after the macro is finished will the project recompile, but that is too late.
    If you remove MyModule in one procedure and at the end of this procedure add the running of a macro that imports the new module, but using the OnTime method, the whole task will be done in two threads. In the first thread you delete the module, and in the second thread you import the new one. Between the first and second threads there will be a "break" in which recompilation will take place. The second recompilation will take place after the second thread finishes.

    BOOM!

    Artik

  9. #9
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Harrisburg, PA
    MS-Off Ver
    Office 365 MSO (16.0.10730.20264) 32-bit
    Posts
    160

    Re: Delete code module and import same name

    Thanks Artik... I will give it a try and report back. Run a routine to delete all vb module... set an on timer event after the vb code is deleted... the on timer event will import the new modules, Correct?

  10. #10
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Harrisburg, PA
    MS-Off Ver
    Office 365 MSO (16.0.10730.20264) 32-bit
    Posts
    160

    Re: Delete code module and import same name

    I wanted to give an update... I did a combination of answers above.
    Existing user file (#0). Copy of #1 at various stages of rev updates to make it match current #1. Since I already started updating for users, I had a path already set... Files can be in various stages of rev changes.
    Main Template file (#1) stored on a sharepoint repository. Most up to date file. New users pull this one.
    Users copy of #1 (#2) stored on the users system or sharepoint or onedrive or anywhere
    Code Reference File (#3). sored in the same location as #1

    To summarize where I ended up. When #0 opens, it first does a code rev check (Rev of #0 is stored in a cell in #0) against sql server. If a code rev update is needed, #0 opens #3 hidden, then calls a routing in #0 that sets up an ontimer routine to run from #3. #3 routine unlocks vb code in #0 and imports all modules (stored on a sharepoint site) into #0, writes the new rev from sql server into rev tab of #0, writes the new rev in cell in #0 , saves #0 and closes. Code Rev modules are upto date on the sharepoint so I does not matter if #0 misses code rev update "b" if current rev is "c". The most up to date code rev is imported.

    When #0 opens next time, code rev matches. Next is file rev. File rev is checked against sql server. If different, macro list to run is read from server. Each file rev update macro is run in order. Then file saves.

    The one thing I learned is that if you are running a macro in a module, issues occur if you try to import into that module... all other imported modules are ok to import.

    Everything works well... a little convoluted, but that happened trying to make it smooth with changes through out.... first time for something like this.

    The one thing I don't know is how to re-lock vb code after unlock and import. Anyone able to help with the re-lock code? I unlock with this code:
    https://stackoverflow.com/questions/...t-from-vb-code
    Thanks.
    At next start up
    Last edited by Steveapa; 03-22-2024 at 09:01 AM.

  11. #11
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Delete code module and import same name

    Hi again,

    Many thanks for the Reputation increase - much appreciated!

    Best regards,

    Greg M

  12. #12
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,224

    Re: Delete code module and import same name

    Quote Originally Posted by Steveapa View Post
    The one thing I don't know is how to re-lock vb code after unlock and import. Anyone able to help with the re-lock code?
    If the project was protected in the file and you unprotected it, the project should still be protected when you save and reopen the file. So it seems that you don't need to do anything except save and close the file where you changed the code.

    Artik

  13. #13
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Harrisburg, PA
    MS-Off Ver
    Office 365 MSO (16.0.10730.20264) 32-bit
    Posts
    160

    Re: Delete code module and import same name

    Thanks Artik. Yes, it is protected on save / close / open. BUt I would prefer to not save / close. If the file is opened from an emial attachment, I dont' wnat to save it . I guess I could ask them where they want to save it.. and default to the current location. I will consider that. Thansk.

+ 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. Import module code to sheets module ?
    By Rmag in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-18-2017, 06:58 AM
  2. [SOLVED] Import module then run code not working.
    By R-Enemy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-14-2015, 06:08 PM
  3. Import Code Module
    By macdonab in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-14-2015, 05:39 PM
  4. 'Multiple Code Module Management' (Export, Import, CleanUp, etc.)
    By Warbe in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-08-2015, 06:17 PM
  5. [SOLVED] Delete Form, Module and This Workbook code (Run Once)
    By Excelnoub in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-15-2014, 12:11 PM
  6. 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

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