使用宏时防止 Excel 中出现“运行时错误‘7’内存不足”错误

使用宏时防止 Excel 中出现“运行时错误‘7’内存不足”错误

每当我在 excel 文件中运行宏时,我都会不断收到此错误。有什么方法可以防止这种情况发生吗?我的代码如下。调试突出显示以下行作为问题:

ActiveSheet.Shapes.SelectAll

我的宏:

Private Sub Save()

Dim sh As Worksheet

ActiveWorkbook.Sheets("Report").Copy 'Create new workbook with Sheets("Report"(2)) as only sheet.
Set sh = ActiveWorkbook.Sheets(1) 'Set the new sheet to a variable. New workbook is now active workbook.
sh.Name = sh.Range("B9") & "_" & Format(Date, "mmyyyy") 'Rename the new sheet to B9 value + date.
With sh.UsedRange.Cells
    .Value = .Value 'eliminate all formulas
    .Validation.Delete 'remove all validation
    .FormatConditions.Delete 'remove all conditional formatting
    ActiveSheet.Buttons.Delete
    ActiveSheet.Shapes.SelectAll
    Selection.Delete

    lrow = Range("I" & Rows.Count).End(xlUp).Row 'select rows from bottom up to last containing data in column I
    Rows(lrow + 1 & ":" & Rows.Count).Delete 'delete rows with no data in column I
    Application.ScreenUpdating = False
    .Range("A410:XFD1048576").Delete Shift:=xlUp 'delete all cells outwith report range
    Application.ScreenUpdating = True

    Dim counter
    Dim nameCount

    nameCount = ActiveWorkbook.Names.Count
    counter = nameCount
    Do While counter > 0
    ActiveWorkbook.Names(counter).Delete
    counter = counter - 1
    Loop 'remove named ranges from workbook

End With
ActiveWorkbook.SaveAs "\\Marko\Report\" & sh.Name & ".xlsx" 'Save new workbook using same name as new sheet.
ActiveWorkbook.Close False 'Close the new workbook.
MsgBox ("Export complete. Choose the next ADP in cell B9 and click 'Calculate'.") 'Display message box to inform user that report has been saved.

End Sub

不确定如何使其更有效或避免出现此错误。

相关内容