[英]Eliminating Screen Flickering in VBA Excel when Copying and Pasting Between Workbooks
我已經制作了一個宏,用於從報表工作簿中復制某些數據並將其粘貼到摘要工作簿中。 從功能上講,宏工作得很好,但是隨着數據在工作簿之間移動,我看到了“閃爍”的效果。 我嘗試了一些技巧來消除它(請參見代碼),但仍然閃爍! 關於如何消除它或可能導致它的任何建議?
我已經引用了類似的問題,但不適用於我的情況。
這是我的代碼的簡短縮寫版本。 我想我已經包括了可能與此問題相關的所有部分,但是請讓我知道是否有任何不合理的地方。
Sub GetInfo()
'This macro copies and pastes certain information from a
'report of a fixed format into a summary with a 'nicer' format.
'Variables
Dim xReport As Workbook
Dim xSummary As Workbook
Dim xReportSheet As Worksheet
Dim xSummarySheet As Worksheet
Dim rng As Range
'Initilizations
Set xSummary = Workbooks("Summary")
Set xSummarySheet = xSummary.ActiveSheet
Set xReport = Workbooks.Open(xFilePath)
Set xReportSheet = xReport.ActiveSheet
'Turn Off Window Flickering (but it doesn't work)
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
Application.DisplayStatusBar = False
Application.DisplayAlerts = False
'Format info in each workbook.
With xSummary
With xSummarySheet
'Do some initial formatting to workbook prior to pasting info.
End With
End With
With xReport
With xReportSheet
'Do some formatting on the info before copying.
End With
End With
'Copy and Paste Data between workbooks.
'Copy
With xReport
With xReportSheet
Set rng = .Cells(2,5)
Application.CutCopyMode = False
rng.Copy
End With
End With
'Paste
With xSummary
With xSummarySheet
Set rng = .Cells(3,1)
rng.PasteSpecial Paste:=xlpasteValues
End With
End With
'Copy and Paste a few more times
'...
'...
'...
'Return to normal
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = True
Application.DisplayStatusBar = True
Application.DisplayAlerts = True
End Sub
謝謝
不需要雙重嵌套的With
語句。 您一次只能使用1個With...End With
語句(嗯,實際上您可以使用先前的with語句來限定一個新的With
語句,但是在這種情況下您不能這樣做)。 無論如何,這不是您的問題。
嘗試查看避免復制/粘貼是否可以滿足您的需要。
替換所有這些:
'Copy and Paste Data between workbooks.
'Copy
With xReport
With xReportSheet
Set rng = .Cells(2,5)
Application.CutCopyMode = False
rng.Copy
End With
End With
'Paste
With xSummary
With xSummarySheet
Set rng = .Cells(3,1)
rng.PasteSpecial Paste:=xlpasteValues
End With
End With
這段代碼:
xSummarySheet.Cells(3, 1) = xReportSheet.Cells(2, 5).Value
如果沒有其他問題,我相信您的代碼至少會運行得更快。
另外,不確定使用的是.Activate
還是.Select
。 如果是,那就不要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.