One of my clients has issue running the Excel VBA code below. He gets the following error Method 'VBE' of object '_Application' failed
, but only once, after he opens the VBE, it starts to work. Also, it was working for him until yesterday.
He is using Excel 2010.
This is the code that throws the error.
For Each f In Application.VBE.ActiveVBProject.VBComponents
If InStr(1, f.Name, "UserForm") = 1 Then
Application.VBE.ActiveVBProject.VBComponents.Remove (f)
End If
Next f
You'll need to enure that Access to the VBE is trusted, by navigating to the Trust Center in Excel Options.
Some actions on the VBComponents require that the VBE has been opened at least once, or at least, that the VBA has referenced the VBE before it tries to enumerate the vbComponents.
It looks like your client's code might be running in an
auto_open
procedure orWorkbook_Open
event. You may find that the code begins to work again if you explicitly add a line the refers to the VBE before you enumerate the vbComponents.See the details in the answers on this question: VBA changing sheet codename Run-Time Error 9: Subscript out of range