VBA:excel正在关闭,没有产生错误
Posted
技术标签:
【中文标题】VBA:excel正在关闭,没有产生错误【英文标题】:VBA: excel is closing, no error generated 【发布时间】:2014-05-21 10:39:37 【问题描述】:我有一个在许多文件上运行的宏。目标是定义一个源并复制我的文件中的值。它适用于 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 替换范围名称借方或贷方,宏将完成,所以我猜这与目的地无关。
我试着把这段代码:
For Each cell In Debit.Cells
MsgBox cell.Value
Next cell
在有问题的线路之前,它通过所有单元格没有任何问题。
我找不到它不工作的任何原因......有什么想法吗?
【问题讨论】:
只是快速浏览一下,但是声明Debit
的 Dim 语句可能没有按照您认为的那样做... Debit
没有明确分配,所以它目前是 @ 987654325@。使用它来确保该行是所有Range
变量:Dim Account_Number As Range, Account_Description As Range, Debit As Range, Credit As Range
很高兴知道。我改变了它,但它没有引起我的问题
您是否尝试一次单步执行代码?
您提到的“问题线”是什么?
在崩溃的 2 行之前的所有变量上尝试 debug.print
【参考方案1】:
首先我认为你应该在你的代码中添加一些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 直到它崩溃(记住它在哪里!)。
【讨论】:
以上是关于VBA:excel正在关闭,没有产生错误的主要内容,如果未能解决你的问题,请参考以下文章
Excel 2007 VBA - 数据透视表字段列表???产生错误