vba - 如何防止Excel中的窗口闪烁/切换?

标签 vba excel

我正在从一个文件复制到另一个文件,并且我可以看到 Excel 在源文件和目标文件之间切换(如闪烁)。我希望宏从源复制并粘贴到目标,而不在文件之间切换(我不想闪烁)。

这里我得到了我的 Excel VBA 代码。我设置了一个按钮来运行 2 个宏。第一个宏是一个 openfiledialog,我可以在其中选择要打开的文件。这是必需的,因为所需的文件总是有不同的名称并且位于不同的路径中。

打开文件后,我的第二个宏启动(CopyPasteValues),即我在下面发布的宏。首先,我设置源工作簿/工作表和目标工作表,并创建一个包含 16 个帐号的数组。

然后我使用 find 方法在每个文件(源文件和目标文件)中搜索帐号。 find 方法的结果用于在 src 文件中创建一个偏移量,并将其复制到目标文件中的一个偏移量。

Sub CopyPasteValues()
    Dim srcWb As Workbook    'source Wb
    Dim srcWs As Worksheet   'source Ws
    Dim trgWb As Workbook    'target Wb
    Dim trgWs As Worksheet   'target Ws

    Set trgWb = ActiveWorkbook
    Set trgWs = trgWb.Sheets("Entry Sheet 20004100")
    Set srcWb = Workbooks.Open(Filename:=openedFile, UpdateLinks:=False, ReadOnly:=True, Editable:=False)
    Set srcWs = srcWb.Sheets("20004100") 

    Dim GLAccountField
    'Array of 16 Account numbers
    GLAccountField = Array(430000, 446030, 477030, 474210, 446075, 472700, 472710, 476000, 476100, 476610, 452200, 454700, 471300, 473110, 490000, 490710)

    Dim srcFinder As Range, trgFinder As Range
    Dim searchGL As Long
    Dim srcRng As Range, trgRng As Range
    Dim i As Integer

    For i = LBound(GLAccountField) To UBound(GLAccountField)

        'The range where GL Accounts will be searched
        Set srcRng = srcWs.Range("A1:A100")    'source file
        Set trgRng = trgWs.Range("B10:B900")   'target file

        'search for the account number(i) in source and target sheets
        searchGL = GLAccountField(i) 
        Set srcFinder = srcRng.Find(searchGL, Lookat:=xlWhole, LookIn:=xlValues, MatchCase:=True)
        Set trgFinder = trgRng.Find(searchGL, Lookat:=xlWhole, LookIn:=xlValues, MatchCase:=True)


        'If finder value equals searched Account Number, then paste to target
        If srcFinder Is Nothing Then
            MsgBox "GL Account: " & searchGL & " NOT found in 'Accounting Input' file"
        Else
            'copy from source
            srcFinder.Offset(0, 15).Resize(1, 12).Copy
            'paste to target from source
            trgFinder.Offset(1, 4).Resize(1, 12).PasteSpecial xlPasteValues  

        End If
    Next i
    srcWb.Close
End Sub

最佳答案

(Andy G 在评论中回答):

在子项目的开头使用Application.ScreenUpdating = False。请记住最后将其设置回 True (在错误处理程序中执行此操作也是一个很好的做法,这样即使在发生错误时它也会被重置):

Sub foo()
  On Error Goto errHandler
  Application.ScreenUpdating = False

  'Your code here

  Application.ScreenUpdating = True
errHandler:
  Application.ScreenUpdating = True
End Sub

关于vba - 如何防止Excel中的窗口闪烁/切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24043988/

相关文章:

vba - Excel VBA : SaveCopyAs with different file extenstion

excel - 复制值而不是公式

excel - VBA 使用 OFT 模板从 Excel 发送电子邮件 - 禁止自动签名

ms-access - MS Access 对两个表进行 dlookup

vba - 如何使用vba锁定Excel单元格中的数据

python - 在 Python 中使用 openpyxl 将行插入 Excel 电子表格

python - QTableWidget联合单元格问题: After jointing a range of cells horizontally,然后我一般联合单元格,所选单元格发疯

excel - 如何使 Excel 用户定义函数 (UDF) 接受字符串类型和范围类型的参数?

vba - 检查项目是否在 ParamArray VBA 中

python - 使用 tempfile 在 Flask 中创建 pdf/xls 文档