如果单元格值等于“空白”,则在Excel中的多个工作表上删除行

如果单元格值为空白,我想删除多个工作表中的行(只在工作簿中的特定行)。 请注意,该行中的其余字段确实包含数据。 到目前为止,我有下面的但不确定如何指定工作表。 谁能帮忙?

Sub sbDelete_rows_if_cell_blank() Dim lRow As Long Dim iCntr As Long lRow = 2000 For iCntr = lRow To 1 Step -1 If Cells(iCntr, 1).Value = "" Then Rows(iCntr).Delete End If Next End Sub 

把你的代码放在这个循环中将循环遍历工作簿中的所有工作表,这些代码在里面并在每个工作表中运行你的代码。

 Sub sbDelete_rows_if_cell_blank() Dim lRow As Long Dim iCntr As Long Dim ws as Worksheet For each ws in ThisWorkbook.Worksheets ' Find last row in column A lRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row For iCntr = lRow To 1 Step -1 If ws.name<>"Sheet1" and ws.name <> "Sheet2" then ' change this line to the sheet names you want to leave out. If IsEmpty(ws.Cells(iCntr, 1)) Or Trim(ws.Cells(iCntr, 1).Value) = "" Then ws.Rows(iCntr).Delete End If end if Next iCntr Next ws End Sub 

if条件更新与D_Bester的build议


更新2:查看评论

这将做我认为你想达到的目标

 Sub Combine() Dim nws, ws As Worksheet Dim rng As Range ' Add New Sheet On Error Resume Next Set nws = ThisWorkbook.Sheets("Combined") If nws Is Nothing Then With ThisWorkbook.Sheets Set nws = .Add(After:=Sheets(.Count)) nws.Name = "Combined" End With End If On Error GoTo 0 For Each ws In ThisWorkbook.Sheets If Not ws.Name = nws.Name Then With ws Set rng = Range(.Cells(1, 1), .Cells(.UsedRange.Rows.Count, .UsedRange.Columns.Count)) rng.Copy Destination:=nws.Cells(nws.Cells(nws.Rows.Count, "A").End(xlUp).Row + 1, 1) End With End If Next ws End Sub 

您可以循环浏览工作表,然后使用特殊单元删除空白。 Yoi还可以设置循环,因此它不会删除“Sheet1”中的空白(在本例中)

 Sub DeleteBlnkRows() Dim sh As Worksheet For Each sh In Sheets If sh.Name <> "Sheet1" Then sh.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete End If Next sh End Sub