简体   繁体   中英

Excel Crashes when workbook is opened

I cannot figure out why my excel workbook file keeps on crashing everytime I open the file.

I have this event handler which I'm sure the one causing the problem.

Option Explicit

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

CurrEntities is declared as public in separate Module.

Public CurrEntities() As Variant

When I try to comment out the line - CurrEntities = Array("Curr1,Ent1", "Curr2,Ent2", "Curr3,Ent3"), the file can be opened without a problem.

So strange because it doesn't give any run time error, it will just prompt a message "Microsoft Excel has stopped working" and then the Excel closes.

Is there something I missed or violated an array variable declaration?

Public CurrEntities() As Variant

means: declare an array of Variant.

Replace with:

Public CurrEntities As Variant

and everything should be OK ;)

Unless... you want to use an array:

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

Cheers,
Maciej

The declaration above and the usage has no problem. I found out and rectified the problem when I tried to create another file and use only the necessary variable for testing. The problem was caused by another variable which is declared incorrectly:

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

I removed this line from my code and the above code worked perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM