Using VBA to insert a Module problem.

dgs2001

Board Regular
Joined
Apr 1, 2007
Messages
139
Hi All
I am trying to auto replace a module in a series of workbooks.

The following is some borrowed code (from internet searches) and some of my own.

The problem is the loop through the directory does not work.
I have tracked the cause to the
InsertVBComponent wbFrom, "D:\Automated Betting\XL Macros\260209Module4.bas"
function call.

Although this is correct it causes the loop command to exit the routine I.E. Although there are twelve workbooks in the directory, after the first workbook is saved and closed the
Code:
F=Dir()
command sets F=""

Can anyone help me get round this?

Duncan

Code:
Sub FileInfo()

    Dim wb As Excel.Workbook
    Set wb = ThisWorkbook
    Dim wbFrom As Workbook
    
    Dim wsF As Excel.Worksheet
    Dim wsT As Excel.Worksheet
    
    Dim r As Integer
    Dim F As String, Directory As String, ToBook As String, runHere As Workbook
    Dim FromBook As String, FromSheet As String, jumpsCount As Long, flatCount As Long, tom As String
   
    Set wsT = wb.Sheets("Records_")
    tom = Format(Now + 1, "dd/mm/yy")
    tom = Replace(tom, "/", "")
    
    Directory = "D:\Data0209 Onwards - Copy"  'wb.Path
    ToBook = wb.Name
    
    r = 2
   
   'Get Directory
   
   F = Dir(Directory & "\")
   Do While F <> ""
   
   If Not F = "dataGrab_" & tom & ".xlsm" Then
   Application.ShowWindowsInTaskbar = False
   Application.ScreenUpdating = False
    
   ' Get Results Here
      
   r = r + 1
   FromBook = (Directory & "\" & F)
   Workbooks.Open Filename:=FromBook, ReadOnly:=False
   Set wbFrom = Workbooks(F)
  
   
   DeleteVBComponent wbFrom, "Module4"
   InsertVBComponent wbFrom, "D:\Automated Betting\XL Macros\260209Module4.bas"
   
    Application.Run F & "!colorCells"
   
   
           For Each wsF In wbFrom.Worksheets
            FromSheet = wsF.Name
            If Not InStr(1, FromSheet, "_") <> 0 Then
                    Call typeTest(wsF.Range("A1"))
                        Select Case raceType
                        
                            Case Is = "jumps"
                            jumpsCount = jumpsCount + 1
                            
                            Case Is = "flat"
                            flatCount = flatCount + 1
                
                        End Select
                                
            End If
         Next
                
   

   '    loop through all results Books
   wbFrom.Save
   wbFrom.Close Savechanges:=True
   End If
   
   F = Dir()
   Loop
    
   Application.ShowWindowsInTaskbar = True
   Application.ScreenUpdating = True
   Debug.Print jumpsCount
   Debug.Print flatCount
   End Sub


  Function InsertVBComponent(ByVal wkb As Workbook, ByVal CompFileName As String)
' inserts the contents of CompFileName as a new component in wb
' CompFileName must be a valid VBA component suited for
' import (an exported VBA component)
    If Dir(CompFileName) <> "" Then
        ' source file exist
        On Error Resume Next ' ignores any errors if the project is protected
'        wb.VBProject.VBComponents("Module4").Replace
        wkb.VBProject.VBComponents.Import CompFileName
        ' inserts component from file
        On Error GoTo 0
    End If
    Set wkb = Nothing
End Function


Function DeleteVBComponent(ByVal wb As Workbook, ByVal CompName As String)
' deletes the vbcomponent named CompName from wb
    Application.DisplayAlerts = False
    On Error Resume Next ' ignores any errors
    wb.VBProject.VBComponents.Remove wb.VBProject.VBComponents(CompName)
    ' delete the component
    On Error GoTo 0
    Application.DisplayAlerts = True
End Function
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Here is something which I tried and it seems to work.

1) Create a folder C:\temp\ExcelFiles. Copy all excel files into this folder for which you want to update the modules.

2) Copy the modules file. This has a .bas extension into C:\temp folder. (Not the same folder as excel files). In this example it is called script.bas.

3) Open Excel. Goto Tools - Macros - Security - Trusted Publishers - Select the checkbox "Check Access to Visual Basic Project" - Click Ok.

4) Go back to Tools - Visual Basic Editor and run the code given below in a new module. This should automatically import the .bas file into all Excel sheets in c:\temp\excelfiles folder. Hope this helps.

Code:
Sub ImportModules()
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objfolder = objFSO.getfolder("C:\Temp\ExcelFiles")
    For Each f In objfolder.Files
        Workbooks.Open f.Path, , False
        ActiveWorkbook.VBProject.VBComponents.Import "c:\temp\script.bas"
        ActiveWorkbook.Save
        ActiveWorkbook.Close
    Next
End Sub

5) Once complete go back to Trusted Publishers and untick the box.

Let me know if it works.
 
Upvote 0
Thanks guys I have now sorted this.
The Chip Pearson link is excellent and has given me some more reading material!

Duncan
 
Upvote 0

Forum statistics

Threads
1,222,902
Messages
6,168,938
Members
452,227
Latest member
sam1121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top