Can I overwrite an existing BAS files when importing modules?

MrTeeny

Board Regular
Joined
Jul 26, 2017
Messages
238
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:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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!
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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