VBA:excel正在关闭,没有生成错误

时间:2014-05-21 10:39:37

标签: excel vba excel-vba

我有一个宏,我在很多文件上运行。目标是定义源并复制文件中的值。它适用于30个源文件,但我最近有一个使我的Excel崩溃,没有任何错误消息。

这里是代码:

'dimensioning of the variables
'range and workbook
Dim Target_Area As Range
Dim Account_Number, Account_Description, Debit, Credit As Range
Dim General_Balance As Workbook
Dim Transform_file As Workbook
Dim Source_Range As Range

'technical var
Dim LastCell As Range
Dim LastCellNumber As Long
Dim Array_Position As Integer
Dim Worksheet_general_balance As Long
Dim Links As Variant
Dim address As String

'var used to adapt to the different trial balance
Dim startline, account_column, description_column, debit_column, credit_column As Integer
Dim column_to_test As String
Dim Target_Column(0 To 3) As Integer

'setting the variables
address = "blabla"
startline = 5
account_column = 1
description_column = 2
debit_column = 3
credit_column = 4
column_to_test = "A"
Target_Column(0) = 1 
Target_Column(1) = 4 
Target_Column(2) = 5 
Target_Column(3) = 6 
Worksheet_general_balance = 1 

Set Transform_file = ActiveWorkbook
Set General_Balance = Workbooks.Open(address)

With General_Balance.Worksheets(Worksheet_general_balance)
    Set LastCell = .Cells(.Rows.Count, column_to_test).End(xlUp)
    LastCellNumber = LastCell.Row

End With

MsgBox "General TB sheet name: " & General_Balance.Worksheets(Worksheet_general_balance).Name

'3. save the required range from the source file
General_Balance.Worksheets(Worksheet_general_balance).Activate
Set Account_Number = General_Balance.Worksheets(Worksheet_general_balance).Range(Cells(startline, account_column), Cells(LastCellNumber, account_column))
Set Account_Description = General_Balance.Worksheets(Worksheet_general_balance).Range(Cells(startline, description_column), Cells(LastCellNumber, description_column))
Set Debit = General_Balance.Worksheets(Worksheet_general_balance).Range(Cells(startline, debit_column), Cells(LastCellNumber, debit_column))
Set Credit = General_Balance.Worksheets(Worksheet_general_balance).Range(Cells(startline, credit_column), Cells(LastCellNumber, credit_column))

'copying the value to the file
Transform_file.Activate
Transform_file.Worksheets("general balance").Range(Cells(6, Target_Column(0)), Cells(LastCellNumber - startline + 6, Target_Column(0))).Value = Account_Number.Value
Transform_file.Worksheets("general balance").Range(Cells(6, Target_Column(1)), Cells(LastCellNumber - startline + 6, Target_Column(1))).Value = Account_Description.Value

'up to this point, everything works well


'THE FOLLOWING TWO LINES EITHER ONE OF THEM MAKE EXCEL CRASH
Transform_file.Worksheets("general balance").Range(Cells(6, Target_Column(2)), Cells(LastCellNumber - startline + 6, Target_Column(2))).Value = Debit.Value
Transform_file.Worksheets("general balance").Range(Cells(6, Target_Column(3)), Cells(LastCellNumber - startline + 6, Target_Column(3))).Value = Credit.Value



 General_Balance.Close

如果我用Account_Number替换范围名称Debit或Credit,那么宏将完成,所以我猜它不是关于目的地。

我试着把这段代码:

For Each cell In Debit.Cells

MsgBox cell.Value

Next cell

在有问题的行之前,它会毫无问题地通过所有单元格。

我找不到任何理由为什么它不起作用......任何想法?

1 个答案:

答案 0 :(得分:0)

首先,我认为您应该在代码中添加一些On Error,包括一个 MsgBox Err.Description,,Err.Number
我的第一个猜测是你正在尝试写入已经打开/锁定的文件。

    Sub test()
        On Error GoTo Hell
        'Do lots of things
        '...
    Adios:
        Exit Sub

    Hell:
        MsgBox Err.Description, vbCritical, "Error " & Err.Number
        Resume Adios
        Resume

    End Sub

使用上面的示例,当您收到消息框时,按Ctrl + Break,将黄点从resume Adios移动到Resume行,然后按F8。现在你处于导致错误的界限上。

另一种方法是在调试模式下启动Sub,然后按F8直到它崩溃(并记住它在哪里!)。