Can I overwrite an existing BAS files when importing modules?

MrTeeny

Board Regular
Joined
Jul 26, 2017
Messages
234
My current code simply adds a 1 to the module's names, just wondering if there's a replace command I could use or some other method, Thanks

Code:
Sub Import_basFiles()    
Dim StrFile As String
    StrFile = Dir("C:\users\steve\desktop\basfiles\")
    Do While Len(StrFile) > 0
        Application.VBE.ActiveVBProject.VBComponents.Import ("C:\users\steve\desktop\basfiles\" & StrFile)


        StrFile = Dir
    Loop
End Sub
 
Last edited:

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,294
Try...

Code:
Option Explicit

Sub Import_basFiles()


    Dim sourcePath As String
    Dim basFileName As String
    Dim basFileNameNoExt As String
    
    sourcePath = "c:\users\steve\desktop\basfiles\"
    If Right(sourcePath, 1) <> "\" Then
        sourcePath = sourcePath & "\"
    End If
    
    basFileName = Dir(sourcePath & "*.bas")
    
    On Error Resume Next
    Do While Len(basFileName) > 0
        With Application.VBE.ActiveVBProject.VBComponents
            basFileNameNoExt = Left(basFileName, InStrRev(basFileName, ".bas", , vbTextCompare) - 1)
            .Remove .Item(basFileNameNoExt)
            .Import sourcePath & basFileName
        End With
        basFileName = Dir
    Loop
    On Error GoTo 0
    
End Sub

Hope this helps!
 

MrTeeny

Board Regular
Joined
Jul 26, 2017
Messages
234
Thank you , all seems to be working fine and I can also utilise the coding to delete certain modules too, thanks for the help.
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,108,523
Messages
5,523,370
Members
409,514
Latest member
MarkZuckerberg

This Week's Hot Topics

Top