Excel VBAsorting更有效

我目前有一个sorting脚本,将逐行检查某个string。 如果string匹配,那么该行将被复制到该工作表中,该行将被删除。 但是,有时我需要sorting数以万计的行,这可能需要很长时间或将崩溃的Excel。 有没有办法重写它能够更有效地sorting?

在我的代码中,我使用不同的表名复制了多个ws2

Dim ws1 As Worksheet, ws2 As Worksheet Dim firstRowWs1 As Long Dim lastRowWs1 As Long Dim lastRowWs2 As Long Dim searchColumnWs1 As Integer Dim i As Integer Dim check As Variant Dim strSearch As Variant Dim foundMatches As Boolean Dim sDate As String sDate = Format(Now(), "mm/dd/yyyy") Set ws1 = Worksheets("Sort") Set ws3 = Worksheets("Overview") Set ws2 = Worksheets("sheet2") With ws2 lastRowWs1 = ws1.UsedRange.Rows.Count lastRowWs2 = ws2.UsedRange.Rows.Count firstRowWs1 = 1 searchColumnWs1 = 10 strSearch = Array("john") For i = firstRowWs1 To lastRowWs1 For Each check In strSearch If check = ws1.Cells(i, searchColumnWs1).Value Then ws1.Rows(i).Copy (ws2.Rows(lastRowWs2 + 1)) ws2.Rows(lastRowWs2 + 1).Columns("A:B").Insert xlToRight lastRowWs2 = lastRowWs2 + 1 ws1.Rows(i).Delete shift:=xlUp i = i - 1 Exit For End If Next check Next i End With 

这种挑战很容易用两个数据透视表来解决:数据透视表1有一个filter只显示你想要的东西,数据透视表1有一个filter显示其他的一切。 没有VBA,闪电般快速,如果需要的话,用VBA脚本很容易。

PowerQuery(又名Get&Transform)也可能让你解决这个问题。