我是這個論壇的新手,但最近我一直在閱讀大量的帖子,因爲我目前正在自學VBA的工作中使用!運行兩次內存時出現內存不足的問題
我目前遇到了一些我已經創建的代碼的問題。代碼的目的是根據雙擊單元格值自動篩選多個工作表,然後將這些過濾的結果複製到另一個「主報表」工作表。問題是它一次運行得很好,在此之後,如果我嘗試再次運行它或者在工作簿中運行其他任何宏,會彈出一個錯誤,要求我關閉內存以釋放內存!
我試着運行一次宏,保存並關閉工作簿(清除任何可能被緩存的內容),重新打開並運行,但同樣的錯誤仍然存在。我也試圖改變我的。選擇與.activate提示所建議:
How to avoid running out of memory when running VBA
但似乎打破我的代碼......後來我又可能只是實現了它錯了,因爲我是一個有點VBA noob任何人都可以幫助我優化我的代碼,以防止這種情況?
我的代碼如下:
Private Sub Merge()
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Selection.Merge
End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Application.ScreenUpdating = False
Application.EnableEvents = False
Sheets("Master Report").Cells.Delete 'clear old master report
Column = Target.Column
Row = Target.Row
'this automatically filters information for a single part and creates a new master report with summary information
PartNumber = Cells(Row, 2).Value 'capture target part number for filtering
PartDesc = Cells(Row, 7).Value 'capture target part description
PartNumberWildCard = "*" & PartNumber & "*" 'add wildcards to allow for additional terms
With Worksheets("NCR's") 'filter NCR sheet
.Select
On Error Resume Next
ActiveSheet.ShowAllData 'remove any previous filters
On Error GoTo 0
.Range("A1").AutoFilter Field:=2, Criteria1:=PartNumberWildCard
End With
Sheets("NCR's").Select
Sheets("NCR's").Range("A3:K3").Select
Sheets("NCR's").Range(Selection, Selection.End(xlDown)).Select 'select NCR filtered summary info
Selection.Copy
Sheets("Master Report").Select
Sheets("Master Report").Range("A1").Formula = PartNumber
Sheets("Master Report").Range("D1").Formula = PartDesc 'Print part no. & description at top of master report
Sheets("Master Report").Range("A4").Select
ActiveSheet.Paste 'paste filtered NCR info into master report
Sheets("Master Report").Range("A3:K3").Select
Call Merge
ActiveCell.FormulaR1C1 = "NCR's"
With Worksheets("CR's") 'filter CR sheet
.Select
On Error Resume Next
ActiveSheet.ShowAllData 'remove any previous filters
On Error GoTo 0
.Range("A1").AutoFilter Field:=3, Criteria1:=PartNumberWildCard
End With
Sheets("CR's").Select
Sheets("CR's").Range("A7:F7").Select
Sheets("CR's").Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Master Report").Select
Sheets("Master Report").Range("P4").Select
ActiveSheet.Paste
Sheets("Master Report").Range("RP3:U3").Select
Call Merge
ActiveCell.FormulaR1C1 = "CR's"
With Worksheets("PO's") 'filter PO sheet
.Select
On Error Resume Next
ActiveSheet.ShowAllData 'remove any previous filters
On Error GoTo 0
.Range("A1").AutoFilter Field:=2, Criteria1:=PartNumberWildCard
End With
Sheets("PO's").Select
Sheets("PO's").Range("A3:H3").Select
Sheets("PO's").Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Master Report").Select
lastRow = Sheets("Master Report").Range("A" & Rows.Count).End(xlUp).Row
lastRow = lastRow + 3
Sheets("Master Report").Range("A" & lastRow).Select
ActiveSheet.Paste
Sheets("Master Report").Range("A" & lastRow - 1 & ":H" & lastRow - 1).Select
Call Merge
ActiveCell.FormulaR1C1 = "PO's"
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
信息的另一塊,可以幫助是,我試圖消除最後三個濾/複製/粘貼套路,這讓我來運行代碼約3在運行到相同的內存錯誤之前的時間。另外,調試器總是被卡住的命令來清除在宏觀
Sheets("Master Report").Cells.Delete 'clear old master report
我還要補充的宏'Application.CutCopyMode = False'的結尾來清除剪貼板。 –
[避免使用'.Select'](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros),這可能會導致緩慢起伏和錯誤的行爲,如果你不小心 – BruceWayne