打开Excel文档时出现VBA错误

时间:2014-04-16 09:41:24

标签: excel vba excel-vba

我为我的xl表添加了一些VBA代码。 我打开WorkBook时似乎编译了这段代码。 我打开文档时得到的错误类型的例子。

Private Sub ComboBox1_Change()
If ComboBox1.Value = "GGS" Then
Sheets("Index").CommandButton2.Visible = True
==> Sheets("Index").Label6.Visible = True
Sheets("MNO").CommandButton5.Visible = True
Sheets("ServiceProvider").CommandButton5.Visible = True
Sheets("ServiceDeployer").CommandButton5.Visible = True
Sheets("CardVendor").CommandButton5.Visible = True
Sheets("LoadFile").CommandButton5.Visible = True

Else

Sheets("Index").Label6.Visible = False
Sheets("Index").CommandButton2.Visible = False
Sheets("MNO").CommandButton5.Visible = False
Sheets("ServiceProvider").CommandButton5.Visible = False
Sheets("ServiceDeployer").CommandButton5.Visible = False
Sheets("CardVendor").CommandButton5.Visible = False
Sheets("LoadFile").CommandButton5.Visible = False
End If

我得到了一个"运行时错误''' :对象不支持属性或方法"在标记的线上 有时在另一张表中该行存在相同的错误

Me.ListBox1.Clear

所以我结束了debuging,然后所有代码都正常工作,甚至是最后一行。

是否可以在文档打开时禁用自动编译?

似乎VBA编辑器尝试在创建View之前运行代码。 调试只需删除"所需的对象" 。我在停止调试之前检查过,对象不在对象列表中。当我停止调试然后检查列表时,对象就在那里

(我是法国人,所以可能会有一些英语错误)

感谢您阅读我

Spikeze

编辑: 我发现了问题。

" ComboBox1.Style"在fmStyleDropDownList上,以避免用户编辑选项。 但是此选项使Combobox在文档打开时自动选择第一个选项。 所以我想它会运行" ComboBox1_Change()"当选择第一个选项但此时未加载某些视图元素且VBA编辑器删除" Objec required"。

当工作表处于活动状态时,我将样式设置为fmStyleDropDownCombo和fmStyleDropDownList,但如果工作簿已保存并重新打开,则样式再次出现在fmStyleDropDownList上,我再次收到错误。

为了解决这个问题,我有了那些

Private Sub Worksheet_Activate()
ComboBox1.Style = fmStyleDropDownList
End Sub

在索引表代码和

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Index").ComboBox1.Style = fmStyleDropDownCombo
End Sub

在WorkBook代码中。

Spikeze

1 个答案:

答案 0 :(得分:0)

您可以使用错误处理。不是最好的解决方案,但它应该工作。如果您确定存在这些对象,则在需要时可以使用

On Error Resume Next

或者正确处理错误,检查代码错误并仅在438

上恢复
相关问题