我正在使用以下 VBA 腳本。宏的目的是避免每次用戶雙擊/向下鉆取資料透視表值時創建新作業表。相反,腳本將資料復制到專用的“DrillDown”表。
我遇到的問題是單擊幾次后,我收到一個 Excel 錯誤,指出我的記憶體不足。原始資料集不是很大,所以我想知道腳本是否有問題,或者我需要進一步添加一些東西?也許我需要先清除一些臨時資料?
我是 VBA 的菜鳥,所以您的幫助將不勝感激!
需要注意的是,這是一個 Power Pivot 表。
我的代碼:
模塊1
Public CS$
本練習冊
Private Sub Workbook_NewSheet(ByVal Sh As Object)
If CS <> "" Then
With Application
ScreenUpdating = False
Dim NR&
With Sheets("DrillDown")
'Set this to always start at the top of the page
NR = 1
'..and to clear the Drilldown tab..
.Cells.ClearContents
'instead of this..
' If WorksheetFunction.CountA(.Rows(1)) = 0 Then
' NR = 1
'Else
' NR = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 2
'End If
Range("A4").CurrentRegion.Copy .Cells(NR, 1)
End With
.DisplayAlerts = False
ActiveSheet.Delete
.DisplayAlerts = True
'Below is commented out to stop user being returned to Pivot
' Sheets(CS).Select
.ScreenUpdating = True
End With
End If
End Sub
uj5u.com熱心網友回復:
它可能是在資料仍在寫入作業表時觸發事件。您可以保留新創建的作業表并洗掉以前的作業表以避免復制。
Private Sub Workbook_NewSheet(ByVal Sh As Object)
If CS = "" Then Exit Sub
With Application
.DisplayAlerts = False
On Error Resume Next
Sheets("DrillDown").Delete
Sh.Name = "DrillDown" ' renamenew sheet
On Error GoTo 0
.DisplayAlerts = True
End With
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/402977.html
標籤:
上一篇:將集合的集合轉換為范圍
