【问题标题】:Using IsNumeric to delete rows of data使用 IsNumeric 删除数据行
【发布时间】:2018-03-20 19:27:59
【问题描述】:

我有两列数据正在使用 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) = False Then
        Cells(cell, 1).Select
        Rows(cell).EntireRow.Delete
        Exit For
    End If
Next cell

感谢任何帮助!

【问题讨论】:

    标签: excel vba isnumeric


    【解决方案1】:

    只使用

        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
    

    【讨论】:

      【解决方案2】:

      从底部循环

      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
      

      【讨论】:

        【解决方案3】:

        删除(或添加,就此而言)行时,您需要向后循环遍历数据集 - 请参阅@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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-05
          • 2019-02-03
          • 1970-01-01
          • 2018-05-07
          • 2015-09-21
          • 1970-01-01
          相关资源
          最近更新 更多