VBA删除行if

我想要做的就是优化我目前的删除行代码。 在这个阶段,这一步花费了很多时间。

Dim miesiac2 As Integer '--->current month miesiac2 = Range("b1").Value Dim LastRow As Long LastRow = [A65536].End(xlUp).Row For i = LastRow To 1 Step -1 If Cells(i, 1) = miesiac2 Then Rows(i & ":" & i).EntireRow.Delete Next i 

所以…如果A列等于当前月份EntireRow.Delete任何想法?

这是我迄今为止所build立的:

 Option Explicit Public Sub TestMe() Application.ScreenUpdating = False Dim miesiac2 As Long Dim LastRow As Long Dim i As Long Dim rRange As Range miesiac2 = Range("b1").Value LastRow = [A65536].End(xlUp).Row 'xl2003 For i = LastRow To 1 Step -1 If Cells(i, 1) = miesiac2 Then If rRange Is Nothing Then Set rRange = Rows(i) Else Set rRange = Union(rRange, Rows(i)) End If End If Next i If Not rRange Is Nothing Then rRange.Select Application.ScreenUpdating = True End Sub 

它使用一个Union ,它select行而不是删除它们。 它的可见性的原因,但你可以修复它。 此外,65K行只在Excel 2003中,在以后的版本中,行是1Mln +。 最后但并非最不重要的 – 不要在Excel中使用integer ,其缓慢和危险。

这是我可以匆匆做的

 Sub delete_on_condition() Dim wb_export As Workbook Dim wb_export_sheet As Worksheet Dim arr_raw_dump As Variant Dim arr_final Dim findcell As Range Set wb_export = ThisWorkbook ' CHANGE IT IF REQURIED Set wb_export_sheet = wb_export.Sheets(1) 'CHANGE IT IF REQUIRED Dim ctr As Long ctr = 0 With wb_export_sheet.Range("A1").CurrentRegion ' OR With wb_export_sheet.USEDRANGE Do Set findcell = .Find("SOME TEXT") If ctr = 0 And findcell Is Nothing Then MsgBox "No data found" Exit Sub End If wb_export_sheet.Rows(findcell.Row).Delete Set findcell = .Find("SOMETEXT") ctr = ctr + 1 Loop While Not findcell Is Nothing End With End Sub