Vba 如何删除包含非数值的所有行
我有一张这样的表格,上面有填充的列Vba 如何删除包含非数值的所有行,vba,excel,Vba,Excel,我有一张这样的表格,上面有填充的列 Column B Column C Column D row 2 : Product 1 100 200 300 row 3 : Product 2 100 200A 100 row 4 : Product 3 AAA 200 300 row 5 : Product
Column B Column C Column D
row 2 : Product 1 100 200 300
row 3 : Product 2 100 200A 100
row 4 : Product 3 AAA 200 300
row 5 : Product 4 600 200 300
row 6 : Product 5 150 200 300,2A
我想要的是:
对于每一行,如果columnB的值不是数字或columnC的值不是数字或columnD的值不是数字,则删除该行
这是我的代码,我觉得很好:
Dim xWs As Worksheet
Set xWS = ThisWorkbook.Sheets("sheet1")
lastrow = xWs.Cells(Rows.Count, 2).End(xlUp).Row
For i = 2 To lastrow
BValue = xWs.Range("B" & i).Value
CValue = xWs.Range("C" & i).Value
DValue = xWs.Range("D" & i).Value
If Not IsNumeric(BValue) Or Not IsNumeric(CValue) Or Not IsNumeric(DValue) Then
xWs.Rows(i).Delete
End If
Next i
我的问题是:当我执行代码时。。它删除第4行和第6行,但保留第3行,即使columnB值不是数字
我想不出这个问题。。
提前谢谢 您可以一次性使用Union和delete,通过向上删除行来避免跳过行。您应该向后删除行
Option Explicit
Sub test()
Dim xWs As Worksheet, i As Long, lastRow As Long, unionRng As Range
Set xWs = ThisWorkbook.Sheets("sheet1")
lastRow = xWs.Cells(xWs.Rows.Count, 2).End(xlUp).Row
Dim BValue As Variant, CValue As Variant, DValue As Variant
For i = 2 To lastRow
BValue = xWs.Range("B" & i).Value
CValue = xWs.Range("C" & i).Value
DValue = xWs.Range("D" & i).Value
If Not IsNumeric(BValue) Or Not IsNumeric(CValue) Or Not IsNumeric(DValue) Then
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, xWs.Rows(i))
Else
Set unionRng = xWs.Rows(i)
End If
End If
Next i
If Not unionRng Is Nothing Then unionRng.Delete
End Sub
或者倒退:
Option Explicit
Sub test()
Dim xWs As Worksheet, i As Long, lastRow As Long, unionRng As Range
Set xWs = ThisWorkbook.Sheets("sheet1")
lastRow = xWs.Cells(xWs.Rows.Count, 2).End(xlUp).Row
Dim BValue As Variant, CValue As Variant, DValue As Variant
For i = lastRow To 2 Step -1
BValue = xWs.Range("B" & i).Value
CValue = xWs.Range("C" & i).Value
DValue = xWs.Range("D" & i).Value
If Not IsNumeric(BValue) Or Not IsNumeric(CValue) Or Not IsNumeric(DValue) Then xWs.Rows(i).Delete
Next i
End Sub
这里有一个最快的方法,它不会在行中循环。。。它使用
SpecialCells
和Autofilter
Sub Sample()
Dim ws As Worksheet
Dim rng As Range, delRng As Range, totRng As Range
Dim lRow As Long
Dim i As Long
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
'~~> Find the last row
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
Set rng = .Range("B2:D" & lRow)
Set totRng = .Range("B1:D" & lRow)
On Error Resume Next
Set delRng = rng.SpecialCells(xlCellTypeConstants, 2)
On Error GoTo 0
If delRng Is Nothing Then Exit Sub _
Else delRng.ClearContents
For i = 1 To 3
.AutoFilterMode = False
With totRng
.AutoFilter Field:=i, Criteria1:="="
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
.AutoFilterMode = False
Next i
End With
End Sub
我的假设(视情况而定)
Sheet1
只是一句话。。也许您想在第二个代码中的“next i”之前添加一个“end if”。^^再次感谢..嗨,如果If语句在一行上,那么它不会以End If终止。结束If是多行If语句。感谢您的回答。。它没有按预期工作。。它甚至会删除包含数值的行。如果您的数字存储为文本,则它们将被删除。我将发现问题。。由于我的数据从第2行开始,代码中的行:Set-totRng=.Range(“B1:D”和lRow)应该是:Set-totRng=.Range(“B2:D”和lRow)。。现在它工作得很好。。谢谢你^^