Excel在打开工作簿时崩溃

时间:2014-12-31 06:56:11

标签: vba

我无法弄清楚为什么每次打开文件时我的excel工作簿文件都会一直崩溃。

我有这个事件处理程序,我确定导致问题的处理程序。

Option Explicit

Private Sub Workbook_Open()
    On Error Resume Next
    CurrEntities = Array("Curr1,Ent1", "Curr2,Ent2", "Curr3,Ent3")
End Sub

CurrEntities在单独的模块中声明为公共。

Public CurrEntities() As Variant

当我尝试注释掉这一行时--CurEEntities = Array(" Curr1,Ent1"," Curr2,Ent2"," Curr3,Ent3"),可以毫无问题地打开文件。

如此奇怪,因为它没有给出任何运行时错误,它只会提示一条消息" Microsoft Excel已停止工作"然后Excel关闭。

我是否遗漏或违反了数组变量声明?

2 个答案:

答案 0 :(得分:1)

Public CurrEntities() As Variant

表示:声明一个Variant数组。

替换为:

Public CurrEntities As Variant

一切都应该没问题;)

除非......你想使用数组:

Public CurrEntities() As Variant

Sub Test()
Dim i As Integer, j As Integer
Dim curent As Variant

CurrEntities = Array(Array("a", "b"), Array("c", "d"))

For i = LBound(CurrEntities()) To UBound(CurrEntities)
    curent = CurrEntities(i)
    Debug.Print "---=== " & i & " ===---"
    For j = LBound(curent) To UBound(curent)
        Debug.Print curent(j)
    Next
Next

End Sub

干杯,
马切伊

答案 1 :(得分:0)

上面的声明和用法没有问题。当我尝试创建另一个文件并仅使用必要的变量进行测试时,我发现并纠正了这个问题。问题是由另一个声明错误的变量引起的:

Public CoCodes("00123", "00456", "00789") As String

我从代码中删除了这一行,上面的代码完美无缺。