Excel 使用IsNumeric删除数据行
我有两列数据我正在清理使用VBA。如果A列中的值不是数字或为空,则需要删除整行。下面是我试图使用的数据和代码示例。如果IsNumeric返回false,它似乎完全跳过了删除行的代码部分Excel 使用IsNumeric删除数据行,excel,vba,isnumeric,Excel,Vba,Isnumeric,我有两列数据我正在清理使用VBA。如果A列中的值不是数字或为空,则需要删除整行。下面是我试图使用的数据和代码示例。如果IsNumeric返回false,它似乎完全跳过了删除行的代码部分 9669 DONE 9670 OPEN Order # STATUS 9552 9672 不起作用的代码 Dim cell As Range For Each cell In Range("A1:A" & max_col) If IsNumeric(cell) =
9669 DONE
9670 OPEN
Order # STATUS
9552
9672
不起作用的代码
Dim cell As Range
For Each cell In Range("A1:A" & max_col)
If IsNumeric(cell) = False Then
Cells(cell, 1).Select
Rows(cell).EntireRow.Delete
Exit For
End If
Next cell
感谢您的帮助 从底部循环
Dim max_col as long
max_col = 100
Dim i as Long
For i = max_col to 1 step -1
If Not isnumeric(activesheet.cells(i,1)) then
activesheet.rows(i).delete
End If
Next i
从底部循环
Dim max_col as long
max_col = 100
Dim i as Long
For i = max_col to 1 step -1
If Not isnumeric(activesheet.cells(i,1)) then
activesheet.rows(i).delete
End If
Next i
在删除(或添加)行时,您需要在数据集中向后循环-请参阅@ScottCraner的示例以获得类似的确切答案-或者,您创建要删除的单元格范围,然后立即删除,如下所示:
Dim rowNo as Long
For rowNo = 1 to max_col
Dim cell as Range
Set cell = Range(rowNo,1)
If IsNumeric(cell) Then
Dim collectRows as Range
If collectRows is Nothing Then
Set collectRows = cell
Else
Set collectRows = Union(collectRows,cell)
End If
End If
Next
collectRows.EntireRow.Delete
在删除(或添加)行时,您需要在数据集中向后循环-请参阅@ScottCraner的示例以获得类似的确切答案-或者,您创建要删除的单元格范围,然后立即删除,如下所示:
Dim rowNo as Long
For rowNo = 1 to max_col
Dim cell as Range
Set cell = Range(rowNo,1)
If IsNumeric(cell) Then
Dim collectRows as Range
If collectRows is Nothing Then
Set collectRows = cell
Else
Set collectRows = Union(collectRows,cell)
End If
End If
Next
collectRows.EntireRow.Delete
仅使用
With Range("A1", Cells(Rows.Count, 1).End(xlUp))
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
.SpecialCells(xlCellTypeConstants, xlTextValues).EntireRow.Delete
End With
或者,如果您不确定是否会有空的数字单元格
With Range("A1", Cells(Rows.Count, 1).End(xlUp))
If WorksheetFunction.CountBlank(.Cells) > 0 Then .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
If WorksheetFunction.Count(.Cells) < .Rows.Count Then .SpecialCells(xlCellTypeConstants, xlTextValues).EntireRow.Delete
End With
带范围(“A1”,单元格(行数,1)。结束(xlUp))
如果工作表function.CountBlank(.Cells)>0,则.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
如果工作表function.Count(.Cells)<.Rows.Count,则.SpecialCells(xlCellTypeConstants,xlTextValues).EntireRow.Delete
以
仅使用
With Range("A1", Cells(Rows.Count, 1).End(xlUp))
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
.SpecialCells(xlCellTypeConstants, xlTextValues).EntireRow.Delete
End With
或者,如果您不确定是否会有空的数字单元格
With Range("A1", Cells(Rows.Count, 1).End(xlUp))
If WorksheetFunction.CountBlank(.Cells) > 0 Then .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
If WorksheetFunction.Count(.Cells) < .Rows.Count Then .SpecialCells(xlCellTypeConstants, xlTextValues).EntireRow.Delete
End With
带范围(“A1”,单元格(行数,1)。结束(xlUp))
如果工作表function.CountBlank(.Cells)>0,则.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
如果工作表function.Count(.Cells)<.Rows.Count,则.SpecialCells(xlCellTypeConstants,xlTextValues).EntireRow.Delete
以