+ Reply to Thread
Results 1 to 9 of 9

VBA modules update creates duplicate entries

Hybrid View

o3836002@rtrtr.com VBA modules update creates... 11-13-2012, 10:04 AM
arlu1201 Re: VBA modules update... 11-13-2012, 10:15 AM
Norie Re: VBA modules update... 11-13-2012, 10:26 AM
o3836002@rtrtr.com Re: VBA modules update... 11-13-2012, 10:39 AM
Norie Re: VBA modules update... 11-13-2012, 10:43 AM
o3836002@rtrtr.com Re: VBA modules update... 11-13-2012, 10:45 AM
Kyle123 Re: VBA modules update... 11-13-2012, 10:46 AM
Norie Re: VBA modules update... 11-13-2012, 11:04 AM
o3836002@rtrtr.com Re: VBA modules update... 11-13-2012, 11:22 AM
  1. #1
    Registered User
    Join Date
    11-13-2012
    Location
    Germany
    MS-Off Ver
    Excel 2010
    Posts
    12

    VBA modules update creates duplicate entries

    Hello together,

    I'm trying to update an existing VB module in an excel sheet. My code is as simple as this

    For Each m In ThisWorkbook.VBProject.VBComponents
            If m.name = "Globals" Then
                ThisWorkbook.VBProject.VBComponents.Remove m
            End If
        Next
        Set k = ThisWorkbook.VBProject.VBComponents.Import("c:\Globals.bas")
        DoEvents
    So I remove it if it is present and import it afterwards. If I run the macro from within the VBE everything works as expected. For convenience I created a menu entry which calls that macro. If I start it this way I end up having just one Module named "Globals1" at first. So it seems that the remove didn't work. Another click generates the correct "Globals" entry but then I have tow modules and everything crashes.

    Any ideas how I can solve this? It is kind a interesting that some other modules I try to import the same way work flawlessly. I also tried some wait statements to pause the macro for a second but also no success. Even without the for-each I end up having a "globals1" first. Really crazy!

    Thanks!

    Moderator's Edit: Use code tags when posting code. To do so in future, select your code and click on the # icon at the top of your post window.
    Last edited by arlu1201; 11-13-2012 at 10:14 AM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA modules update creates duplicate entries

    May i ask why you are updating a VB module through a macro?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

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

    Re: VBA modules update creates duplicate entries

    Try saving (and closing) the workbook after you've removed the module.

    Then open it up again and import the module.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    11-13-2012
    Location
    Germany
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: VBA modules update creates duplicate entries

    The macros are used within a team and I want to provide them an easy update mechanism. Every member is working with it's own sheet and imports data into it via a bigger macro that changes from time to time.

    @Norie: I cannot really close the workbook as the menu is created by one of the macros. Is there any other solution for this weird bug? Crazy enough that menu macro can be updated without any problems. :-(

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

    Re: VBA modules update creates duplicate entries

    Instead of removing the module why not remove all the lines from the module and add all the new lines from Global.bas.

  6. #6
    Registered User
    Join Date
    11-13-2012
    Location
    Germany
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: VBA modules update creates duplicate entries

    That could be a solution! Could you maybe provide me some quick lines of code for that?

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: VBA modules update creates duplicate entries

    Why not just have an add-in? Seems easier/less hacky than trying to write code with code

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

    Re: VBA modules update creates duplicate entries

    Try this.
    
    Sub AddProcedureToModule()
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Dim strFilename As String
    Dim FSO As FileSystemObject
    
    Dim ts As TextStream
    Dim strCode As String
    
        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents("Globals")
        Set CodeMod = VBComp.CodeModule
    
        CodeMod.DeleteLines 1, CodeMod.CountOfLines
    
        Set FSO = CreateObject("Scripting.FileSystemObject")
        
        Set ts = FSO.OpenTextFile("C:\Globals.bas", ForReading)
          
        strCode = ts.ReadAll
    
        CodeMod.AddFromString strCode
        
        CodeMod.DeleteLines 1, 3
        
    End Sub

  9. #9
    Registered User
    Join Date
    11-13-2012
    Location
    Germany
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: VBA modules update creates duplicate entries

    Works perfect! Thanks a lot for the quick help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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