对象"_Application"的方法"VBE"失败 [英] Method 'VBE' of object '_Application' failed
问题描述
我的一位客户在运行下面的Excel VBA代码时遇到问题.他收到以下错误Method 'VBE' of object '_Application' failed
,但是只有一次,在打开VBE之后,它开始工作.而且,它一直为他工作直到昨天.
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.
他正在使用Excel 2010.
He is using Excel 2010.
这是引发错误的代码.
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
推荐答案
您需要通过导航到Excel Options中的信任中心"来确保对VBE的访问受到信任.
You'll need to enure that Access to the VBE is trusted, by navigating to the Trust Center in Excel Options.
对VBComponent的某些操作要求至少打开一次VBE,或者至少在VBA尝试枚举vbComponent之前,VBA已引用VBE .
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.
您的客户代码似乎正在auto_open
过程或Workbook_Open
事件中运行.如果您在枚举vbComponents之前在 之前显式添加了引用VBE的行,则可能会发现代码再次开始工作.
It looks like your client's code might be running in an auto_open
procedure or Workbook_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.
Debug.Assert Application.VBE.ActiveVBProject.Name <> vbNullString
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
请参阅有关此问题的答案中的详细信息: VBA更改工作表代号运行时错误9:下标超出范围
See the details in the answers on this question: VBA changing sheet codename Run-Time Error 9: Subscript out of range
这篇关于对象"_Application"的方法"VBE"失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!