在工作簿之间复制和粘贴时,消除了VBA Excel中的屏幕闪烁

时间:2018-10-16 22:32:14

标签: excel vba window copy-paste flicker

我已经制作了一个宏,用于从报表工作簿中复制某些数据并将其粘贴到摘要工作簿中。从功能上讲,宏工作得很好,但是随着数据在工作簿之间移动,我看到了“闪烁”的效果。我尝试了一些技巧来消除它(请参见代码),但仍然闪烁!关于如何消除它或可能导致它的任何建议?

我已引用this similar question,但不适用于我的情况。

这是我的代码的略微版本。我想我已经包括了可能与此问题相关的所有部分,但是请让我知道是否有任何不合理的地方。

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

谢谢

1 个答案:

答案 0 :(得分:0)

不需要双嵌套的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。如果是,那就不要。