我正在使用 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