简单循环采取年龄

我有这样一个简单的循环,用于查看excel中的一个数据块:它首先标识它向下延伸的行数 – 这定义了每一行N上的迭代次数,看看单元格(N,B)是否空 – 如果这样删除那一行。

这似乎不工作,也需要年龄! 我需要的东西很快就做到了。

任何想法都会被夸大

Sub PREBILLvariant2() Dim N As Long For N = 1 To Worksheets("EMEA input").Cells(Rows.Count, "A").End(xlUp).Row If InStr(Cells(N, "B").Value, "") > 0 Then Worksheets("EMEA input").Cells(N, "B").EntireRow.Delete Next N End Sub 

这是另一种方法。 我总是发现过滤是做这些事情的最快方法。

 Public Sub filterThenDelete() Application.ScreenUpdating = False Dim r As Excel.Range Set r = Sheets("EMEA input").UsedRange r.AutoFilter Field:=2, Criteria1:="" Dim deleteRange As Excel.Range Set deleteRange = r.Offset(1, 0).Resize(r.Rows.Count - 1, r.Columns.Count).Cells.SpecialCells(xlCellTypeVisible) deleteRange.EntireRow.Delete Sheets("EMEA input").AutoFilterMode = False Application.ScreenUpdating = True End Sub 

只是一个FYI,对于AutoFilter, Field:=2表示“将筛选条件1中find的筛选器应用于所选范围中的第2列”。 或者干脆“过滤B列的空白值”。

像这样的东西:

 Sub PREBILLvariant3() Dim ws As Worksheet Dim lRows As Long, N As Long Dim rngToDelete As Range Application.ScreenUpdating = False Set ws = Worksheets("EMEA input") lRows = ws.Cells(Rows.Count, "A").End(xlUp).Row For N = 1 To lRows If ws.Cells(N, "B").Value <> "" Then If rngToDelete Is Nothing Then Set rngToDelete = ws.Cells(N, "B") Else Set rngToDelete = Union(rngToDelete, ws.Cells(N, "B")) End If End If Next N rngToDelete.EntireRow.Delete Application.ScreenUpdating = True Set ws = Nothing End Sub 

这将收集B中非空(<>“)所有单元格作为Range ,并在循环之后一次删除行。

检查不空(<>“”)或Len() > 0比使用InStr()更好的IMO,因为你不是在寻找特定的文本。

这是一个比较标准的方法:

它循环遍历A列中的每一行,并删除C列中有空单元格的每一行

 Sub subDeleteRows() Dim lngRow As Long: lngRow = 1 subSpeedUp True Do Until IsEmpty(Sheets("EMEA input").Cells(lngRow, 1)) If IsEmpty(Sheets("EMEA input").Cells(lngRow, 2)) Then Sheets("EMEA input").Cells(lngRow, 2).EntireRow.Delete Else lngRow = lngRow + 1 End If Loop subSpeedUp False End Sub Sub subSpeedUp(startStop As Boolean) If startStop Then Application.ScreenUpdating = False Application.DisplayStatusBar = False Application.Calculation = xlCalculationManual Application.EnableEvents = False ActiveSheet.DisplayPageBreaks = False Else Application.ScreenUpdating = True Application.DisplayStatusBar = True Application.Calculation = xlCalculationAutomatic Application.EnableEvents = True End If End Sub 

如果你有时间,你应该阅读这篇文章: http : //msdn.microsoft.com/en-us/library/office/ff726673.aspx