0

我正在使用 excel VBA 创建一个新工作表,然后将数据从另一个工作表复制到我创建的这个新工作表。然后我将通过删除一些列和文本换行来格式化新工作表。它可以很好地完成工作但是它效率不高:尽管使用了 Application.DisplayAlerts = False,Application.EnableEvents = False,但屏幕仍然闪烁很多。
有什么帮助吗?

Sub ProcessPostingData()
    Dim MyDateTime As String
    Dim szToday As String
    Dim szTime As String
    Dim TD, TM As String
    Dim AfterFilterFinalRow As Long
    Dim lLastRow3nd As Long


    Application.DisplayAlerts = False
    Application.EnableEvents = False

    On Error Resume Next
    Sheets("szTempNow").Delete
    On Error GoTo 0
    Sheets.Add().Name = "szTempNow"

    Worksheets("DATA_PROCESSING").Select

        lLastRow3nd = Cells(1, 6).EntireColumn.Find("*", SearchDirection:=xlPrevious).Row

    'We sort,create sheet with DateTime stamp,copy data to new sheet and format

    ActiveWorkbook.Worksheets("DATA_PROCESSING").Range(Cells(1, 1), Cells(lLastRow3nd, 10)).Sort _
    Key1:=Range("A1"), Header:=xlYes


    With Worksheets("DATA_PROCESSING")
            AfterFilterFinalRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    Sheets("DATA_PROCESSING").Range("A1:J1").Copy Destination:=Sheets("szTempNow").Range("A1")
    Sheets("szTempNow").Range("A2:J" & AfterFilterFinalRow).Value = Sheets("DATA_PROCESSING").Range("A2:J" & AfterFilterFinalRow).Value

    Sheets("DATA_PROCESSING").Range(Cells(2, 1), Cells(AfterFilterFinalRow, 10)).EntireRow.Delete

    'Removing columns not needed and formating
    Sheets("szTempNow").Select

      'With Sheets("szTempNow")
      .Columns("G:G").Delete Shift:=xlToLeft
      .Columns("D:E").Delete Shift:=xlToLeft
    End With

       'With Range(Cells(1, 1), Cells(AfterFilterFinalRow, 10))

           '.HorizontalAlignment = xlGeneral
           '.VerticalAlignment = xlCenter
           '.WrapText = True
           '.ReadingOrder = xlContext
        'End With

       'Range("E2:E" & AfterFilterFinalRow).Columns("E:E").ColumnWidth = 70

    'Rename Sheet with Todays date and Time

    szTime = Format(Time, "h-mm AM/PM")
    szToday = Format(Now(), "dd-mmm-yyyy")
        TD = "D"
        TM = "T"

     MyDateTime = TD & szToday & TD & "_" & TM & szTime & TM

      ActiveSheet.Name = MyDateTime

      Range("K1").Value = ActiveSheet.Name

      Range("K1").Font.Bold = True
        With Range("K1")
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlCenter
            .ReadingOrder = xlContext
      End With

      Application.EnableEvents = False
      Application.DisplayAlerts = True
    End Sub
4

1 回答 1

5

您正在寻找:

Application.ScreenUpdating = False

这有助于阻止屏幕闪烁,还可以加快处理速度。Application.DisplayAlerts禁止显示“此工作簿未保存”的对话框。

于 2013-07-20T19:21:45.040 回答