Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8823331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:19:39+00:00 2026-06-14T06:19:39+00:00

I’ve been experiencing some strange quirks in Excel while programatically removing modules then reimporting

  • 0

I’ve been experiencing some strange quirks in Excel while programatically removing modules then reimporting them from files. Basically, I have a module named VersionControl that is supposed to export my files to a predefined folder, and reimport them on demand. This is the code for reimporting (the problem with it is described below):

Dim i As Integer
Dim ModuleName As String
Application.EnableEvents = False
With ThisWorkbook.VBProject
    For i = 1 To .VBComponents.Count
        If .VBComponents(i).CodeModule.CountOfLines > 0 Then
            ModuleName = .VBComponents(i).CodeModule.Name
            If ModuleName <> "VersionControl" Then
                If PathExists(VersionControlPath & "\" & ModuleName & ".bas") Then
                    Call .VBComponents.Remove(.VBComponents(ModuleName))
                    Call .VBComponents.Import(VersionControlPath & "\" & ModuleName & ".bas")
                Else
                    MsgBox VersionControlPath & "\" & ModuleName & ".bas" & " cannot be found. No operation will be attempted for that module."
                End If
            End If
        End If
    Next i
End With

After running this, I’ve noticed that some modules don’t appear anymore, while some have duplicates (e.g. mymodule and mymodule1). While stepping through the code, it became obvious that some modules still linger after the Remove call, and they get to be reimported while still in the project. Sometimes, this only resulted having the module suffixed with 1, but sometimes I had both the original and the copy.

Is there a way to flush the calls to Remove and Import so they apply themselves? I’m thinking to call a Save function after each, if there’s one in the Application object, although this can cause losses if things go wrong during import.

Ideas?

Edit: changed tag synchronization to version-control.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:19:41+00:00Added an answer on June 14, 2026 at 6:19 am

    This is a live array, you are adding and removing items during iteration thereby changing the index numbers. Try processing the array backwards. Here is my solution without any error handling:

    Private Const DIR_VERSIONING As String = "\\VERSION_CONTROL"
    Private Const PROJ_NAME As String = "PROJECT_NAME"
    
    Sub EnsureProjectFolder()
        ' Does this project directory exist
        If Len(Dir(DIR_VERSIONING & PROJ_NAME, vbDirectory)) = 0 Then
            ' Create it
            MkDir DIR_VERSIONING & PROJ_NAME
        End If
    End Sub
    
    Function ProjectFolder() As String
        ' Ensure the folder exists whenever we try to access it (can be deleted mid execution)
        EnsureProjectFolder
        ' Create the required full path
        ProjectFolder = DIR_VERSIONING & PROJ_NAME & "\"
    End Function
    
    Sub SaveCodeModules()
    
        'This code Exports all VBA modules
        Dim i%, sName$
    
        With ThisWorkbook.VBProject
            ' Iterate all code files and export accordingly
            For i% = 1 To .VBComponents.count
                ' Extract this component name
                sName$ = .VBComponents(i%).CodeModule.Name
                If .VBComponents(i%).Type = 1 Then
                    ' Standard Module
                    .VBComponents(i%).Export ProjectFolder & sName$ & ".bas"
                ElseIf .VBComponents(i%).Type = 2 Then
                    ' Class
                    .VBComponents(i%).Export ProjectFolder & sName$ & ".cls"
                ElseIf .VBComponents(i%).Type = 3 Then
                    ' Form
                    .VBComponents(i%).Export ProjectFolder & sName$ & ".frm"
                ElseIf .VBComponents(i%).Type = 100 Then
                    ' Document
                    .VBComponents(i%).Export ProjectFolder & sName$ & ".bas"
                Else
                    ' UNHANDLED/UNKNOWN COMPONENT TYPE
                End If
            Next i
        End With
    
    End Sub
    
    Sub ImportCodeModules()
        Dim i%, sName$
    
        With ThisWorkbook.VBProject
            ' Iterate all components and attempt to import their source from the network share
            ' Process backwords as we are working through a live array while removing/adding items
            For i% = .VBComponents.count To 1 Step -1
                ' Extract this component name
                sName$ = .VBComponents(i%).CodeModule.Name
                ' Do not change the source of this module which is currently running
                If sName$ <> "VersionControl" Then
                    ' Import relevant source file if it exists
                    If .VBComponents(i%).Type = 1 Then
                        ' Standard Module
                        .VBComponents.Remove .VBComponents(sName$)
                        .VBComponents.Import fileName:=ProjectFolder & sName$ & ".bas"
                    ElseIf .VBComponents(i%).Type = 2 Then
                        ' Class
                        .VBComponents.Remove .VBComponents(sName$)
                        .VBComponents.Import fileName:=ProjectFolder & sName$ & ".cls"
                    ElseIf .VBComponents(i%).Type = 3 Then
                        ' Form
                        .VBComponents.Remove .VBComponents(sName$)
                        .VBComponents.Import fileName:=ProjectFolder & sName$ & ".frm"
                    ElseIf .VBComponents(i%).Type = 100 Then
                        ' Document
                        Dim TempVbComponent, FileContents$
                        ' Import the document. This will come in as a class with an increment suffix (1)
                        Set TempVbComponent = .VBComponents.Import(ProjectFolder & sName$ & ".bas")
    
                        ' Delete any lines of data in the document
                        If .VBComponents(i%).CodeModule.CountOfLines > 0 Then .VBComponents(i%).CodeModule.DeleteLines 1, .VBComponents(i%).CodeModule.CountOfLines
    
                        ' Does this file contain any source data?
                        If TempVbComponent.CodeModule.CountOfLines > 0 Then
                            ' Pull the lines into a string
                            FileContents$ = TempVbComponent.CodeModule.Lines(1, TempVbComponent.CodeModule.CountOfLines)
                            ' And copy them to the correct document
                            .VBComponents(i%).CodeModule.InsertLines 1, FileContents$
                        End If
    
                        ' Remove the temporary document class
                        .VBComponents.Remove TempVbComponent
                        Set TempVbComponent = Nothing
    
                    Else
                        ' UNHANDLED/UNKNOWN COMPONENT TYPE
                    End If
                End If
                Next i
            End With
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.