Home » excel » excel – Unable to refresh VBA code programatically

excel – Unable to refresh VBA code programatically

Posted by: admin May 14, 2020 Leave a comment


I have a routine that looks like this:

Private Sub RefreshByName(strFileType As String)
Dim strFile As String

strFile = Dir(".\*." & strFileType, vbNormal)

Do While Len(strFile) > 0
    Dim strCompName As String
    Dim vbComp As Object
    strCompName = Left(strFile, Len(strFile) - 4)
    'Fails here
    vbComp = ActiveWorkbook.VBProject.VBComponents(strCompName)
    ActiveWorkbook.VBProject.VBComponents.Remove vbComp
    ActiveWorkbook.VBProject.VBComponents.import (strFile)
    strFile = Dir

End Sub

According to most of what I read online, it should be possible to reference the VBComponents by name, but it doesn’t work. What am I missing?

Generally the error I get is Run-time error 438: Object doesn't support this property or method. – that isn’t the error I get on this specific variant of the code – rather this is an error 91: Object variable or With block variable not set.

But I broke up all the declarations to see the exact part of the line that caused the problem. In playing with this to get the exact error message, It suddenly worked on me (inexplicably, to me anyway) I’m trying to find the correct combination now, but any help with the error messages would be appreciated.

How to&Answers:

You are not using Set when you should:

Set vbComp = ActiveWorkbook.VBProject.VBComponents(strCompName)

You might also want to remove the parentheses from .import (strFile).