在删除OLEObjects和隐藏行之后,VBA设置了计算模式后,Excel停止响应

时间:2019-03-14 15:03:10

标签: excel vba performance

我有一个很大的VBA / Excel项目.xlsm文件,当我单击调用RemoveComparison的“删除行”按钮时,突然开始停止响应(被迫强制退出Excel)子奇怪的是,潜水艇偶尔会起作用(并且过去也曾起作用)。好像Excel在调用此RemoveComparison时有时会耗尽内存,而在其他时候却没有。如果我在最后一行中注释掉Application.Calculation = xlAutomatic,问题就解决了。但是,当RemoveComparison正常工作时,大约需要2整秒才能完成,因为我相信当我将计算模式设置为back时,隐藏行或删除OLEObjects会触发其他工作表上的大量重新计算。到xlAutomatic

有没有人发现可能导致Excel停止响应的原因?

这是工作簿的屏幕截图。 “删除行”按钮调用子RemoveComparison(),在这种情况下,子ComboBoxManagerComparison7会删除Comparison7CheckBox(标普500指数中的那个),并且在隐藏单元格(其中包含引用其他工作表的公式)之前也会删除 Sub RemoveComparison() Application.Calculation = xlManual Dim CurrentNumberOfComparisons As Integer With Sheets("Manager") 'get current number of comparisons (to determine which ComboBox, etc. to remove) CurrentNumberOfComparisons = .Range("NumberOfComparisons").Value If CurrentNumberOfComparisons = 1 Then MsgBox ("Min 1 comparison") Exit Sub End If Dim ComboBoxName As String 'determine name of ComboBox to remove ComboBoxName = "ComboBoxManagerComparison" & CurrentNumberOfComparisons Call DeleteComboBox(ComboBoxName, Sheets("Manager")) #If DEBUGREMOVECOMPARISON Then Debug.Print "Deleted ComboBox" #End If Dim CheckBoxName As String 'determine name of CheckBox to remove CheckBoxName = "Comparison" & CurrentNumberOfComparisons & "CheckBox" Call DeleteCheckBox(CheckBoxName, Sheets("Manager")) #If DEBUGREMOVECOMPARISON Then Debug.Print "Deleted CheckBox" #End If Dim ComparisonCellToHideNamedRange As String 'determine which cells to hide ComparisonCellToHideNamedRange = "selectedManagerComparison" & CurrentNumberOfComparisons & "Name" Dim ComparisonCellToHide As Range Set ComparisonCellToHide = .Range(ComparisonCellToHideNamedRange) Range(Rows(ComparisonCellToHide.row), Rows(ComparisonCellToHide.row + 3)).Hidden = True 'update current number of comparisons .Range("NumberOfComparisons").Value = CurrentNumberOfComparisons - 1 #If DEBUGREMOVECOMPARISON Then Debug.Print "Success" #End If End With Application.Calculation = xlAutomatic End Sub Sub DeleteComboBox(ComboBoxName As String, Sheet As Worksheet) With Sheet .OLEObjects(ComboBoxName).Delete End With End Sub Sub DeleteCheckBox(CheckBoxName As String, Sheet As Worksheet) With Sheet .OLEObjects(CheckBoxName).Delete End With End Sub 。如果“删除行”的工作方式与以前一样,再次单击该按钮将删除“ Comparison6”等。

Screenshot

xlManual

编辑:仍不清楚为什么子菜单中从xlAutomatic回到Application.Calculation = xlAutomatic的切换会导致“无响应”,但是如果我注释掉子文本的最后一行{ {1}},然后手动单击“公式”->“计算选项”->“自动”,即使未编辑任何单元格,也会重新计算整个工作表。我了解隐藏单元格会导致重新计算,但是隐藏行是否会导致整个工作表的重新计算(即使隐藏行没有相关项)?

编辑:如果我将ComboBoxes和CheckBoxes移到工作表的另一部分而不是在子菜单中删除它们,则Excel停止崩溃。我不确定为什么删除这些OLEObject会导致Excel输入没有响应。

0 个答案:

没有答案