我擅长于5种不同的工作表。 sheet3和sheet4我想基于单列单元格值删除行。 在工作表3中,如果H2 =“#N / A”和H503 =“#N / A”然后删除整个行,我想基于H列单元格值删除行。 在工作表4中,如果B2 =“ 320857876”,B3 =“ 32085678”,B4 =“ 12133435”,我想基于b列单元格值删除行,则删除B列单元格值以302开头的整个行。 我想从“ C”列中删除所有数据 我的Excel工作表就是这样
Sub Create()
Dim LastRow As Long
Dim i As Long
LastRow = Range("B10000").End(xlUp).Row
For i = LastRow To 1 Step -1
If Range("B" & i) = "#N/A" Then
Range("B" & i).EntireRow.Delete
End If
Next
End Sub
答案 0 :(得分:0)
您在那里有一些要求,您的代码相当轻巧,但是对于其中的#N/A
部分,您不能仅使用value方法测试该文本,这是返回的默认属性范围对象。
Sub Create()
Dim LastRow As Long, i As Long
LastRow = Range("B10000").End(xlUp).Row
For i = LastRow To 1 Step -1
If Range("B" & i).Text = "#N/A" Then
Range("B" & i).EntireRow.Delete
End If
Next
End Sub
...您需要使用.Text
才能使它正常工作,或者,If IsError(Range("B" & i)) Then
是另一种方法。
其余的要求只是逻辑。您代码的其余部分相对来说比较完善,因此您只需要完成它即可。
我希望有帮助。
答案 1 :(得分:0)
Sub delete_rows()
Dim sheet As Worksheet, cell As Range
Count = 1
For Each sheet In ThisWorkbook.Worksheets
If Count = 3 Then
lastrow = sheet.Cells(sheet.Rows.Count, "H").End(xlUp).Row
Set Rng = sheet.Range("H1:H" & lastrow)
For i = Rng.Cells.Count To 1 Step -1
If Application.WorksheetFunction.IsNA(Rng(i).Value) Then
Rng(i).EntireRow.Delete
ElseIf Rng(i).Value = "#NA" Then
Rng(i).EntireRow.Delete
End If
Next
ElseIf Count = 4 Then
lastrow = sheet.Cells(sheet.Rows.Count, "B").End(xlUp).Row
Set Rng = sheet.Range("B1:B" & lastrow)
Debug.Print (Rng(4).Text)
If Rng(2).Value = "320857876" And Rng(3).Value = "32085678" And Rng(4).Value = "12133435" Then
For i = Rng.Cells.Count To 1 Step -1
If Left(Rng(i).Value, 3) = "302" Then
Rng(i).EntireRow.Delete
End If
Next
End If
lastrow = sheet.Cells(sheet.Rows.Count, "C").End(xlUp).Row
Set Rng = sheet.Range("C1:C" & lastrow)
For Each cell In Rng
cell.Value = ""
Next cell
End If
Count = Count + 1
Next
End Sub