Results 1 to 2 of 2

Runtime Error 9 with VBComponents and Dir()

Threaded View

  1. #1
    Registered User
    Join Date
    03-08-2023
    Location
    Denver, Colorado
    MS-Off Ver
    Microsoft 365
    Posts
    1

    Question Runtime Error 9 with VBComponents and Dir()

    Hello,
    I am trying to get VBA to import a folder of .bas files into the PERSONAL.xlsb workbook upon Excel startup to ensure that users always have the most updated macros. Two lines of mine are throwing Runtime Error 9, and after playing around a bit I think it's either because I'm using Dir or there's something wrong with this line: *If IsNull(TargetWB.VBProject.VBComponents(fileNameNoExt)) = True Then Is Nothing Then*, but I'm not sure it that's what's wrong or how to fix it. (When I substitute an existing module name in quotes (no extension) for fileNameNoExt, there is no error in the AutoUpdateBasFiles sub, only in the DeleteVBComponent Sub. When I substitute a non-existent module name in quotes (no extension) for fileNameNoExt, it throws Runtime Error 9 again.) I am a novice with VBA. Any help or ideas would be appreciated.

    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #2 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Sub DeleteVBComponent(ByVal wb As Workbook, ByVal CompName As String)
    
    'Delete the component
    wb.VBProject.VBComponents.Remove wb.VBProject.VBComponents(CompName)   '***THIS LINE THROWS RUNTIME ERROR 9***
    
    End Sub
    
    
    Sub AutoUpdateBasFiles()
    
    'Automate import of all other modules
        
        Dim userID As String
        userID = Environ("UserName")
        Dim destPath As String
        destPath = "C:\Users" & userID & "\AppData\Roaming\Microsoft\Excel\XLSTART"
        Dim sourcePath As String
        sourcePath = "C:\File Path"   '**FilePath removed for the purpose of this post
    
        'Loop through all files in a folder
        Dim fileName As Variant
        fileName = Dir(sourcePath)
        Dim TargetWB As Workbook
        Set TargetWB = Workbooks("PERSONAL.xlsb")
        Dim fileNameNoExt As String
    
        While IsNull(fileName) = False
            fileNameNoExt = Left(fileName, InStrRev(fileName, ".") - 1)
            If IsNull(TargetWB.VBProject.VBComponents(fileNameNoExt)) = True Then 'Check for existence of module '***THIS LINE THROWS RUNTIME ERROR 9***
                Else
                Call DeleteVBComponent(Workbooks("PERSONAL.xlsb"), fileNameNoExt) 'Delete existing module before importing the new one of the same name
            End If
    
            TargetWB.VBProject.VBComponents.Import (sourcePath & fileName) 'Import the new module
            
            'Set the fileName to the next file
            fileName = Dir()
        Wend
    
        'Save the workbook
        Workbooks("PERSONAL.xlsb").Save
    
    End Sub
    
    
    Private Sub Auto_Open() 
        Call AutoUpdateBasFiles
    End Sub
    Last edited by 6StringJazzer; 03-28-2023 at 12:17 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] UBound coll error after converting macro. Runtime error 13. Type mismatch.
    By Elijah in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-30-2020, 05:08 AM
  2. [SOLVED] Runtime Error 1004 + Runtime 5
    By jrean042 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-07-2020, 08:16 AM
  3. [SOLVED] Runtime Error 1004 and Runtime Error 13
    By Brendan_MS in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-21-2020, 10:52 AM
  4. Runtime error 1004 & runtime error 424
    By MrWhiskey in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-29-2017, 08:57 AM
  5. [SOLVED] VBComponents question
    By Frederick Chow in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-20-2006, 03:25 PM
  6. [SOLVED] Changeing codename of worksheet via VBComponents collection
    By Arne in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-13-2005, 09:05 AM
  7. VBE window keeps opening when VBProject.VBComponents are created
    By dougal11179 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-22-2005, 09:48 AM

Tags for this Thread

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