【问题标题】:Excel VBA need to remove all rows with the #REF! valueExcel VBA 需要使用#REF 删除所有行!价值
【发布时间】:2017-12-11 12:21:06
【问题描述】:

我有以下代码在 L 列中查找带有“记录”一词的值,然后将该行复制到单独的工作表中。

我在值为 #REF! 的行上收到类型不匹配错误!出现在 L 列中。

修改以下内容以完全删除这些行然后继续预期功能的最佳方法是什么?

注意LastRow 是 'Long' 变量类型

Sheets("Demand").Rows("1").Copy Sheets("Data").Range("A1")

With Sheets("Demand")

LastRow = .Range("A" & .Rows.Count).End(xlUp).Row

pasteRowIndex = 2

 For r = 1 To LastRow
        If LCase(.Cells(r, "L").Value) Like LCase("Record*") Then
          If UBound(Split(.Cells(r, "L"), ",")) > 0 Then
              i = i + 1
              ReDim v(1 To i)
              v(i) = pasteRowIndex
              End If

          Sheets("Demand").Rows(r).Copy Sheets("Data").Rows(pasteRowIndex)
         pasteRowIndex = pasteRowIndex + 1
      End If
 Next r
End With

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    首先,您需要从最后一行迭代到第一行:

    For r = LastRow to 1 step -1
    
        'Next, add if statement inside the loop checking if cell returns error:
        If IsError(.Cells(r, "L").Value) then
    
            'if so, delete this row:
            .Cells(r, "L").entirerow.delete
    
        '..... the rest of your code as ElseIf part of conditional statement
    
        ElseIf LCase(.Cells(r, "L").Value) Like LCase("Record*") Then
          If UBound(Split(.Cells(r, "L"), ",")) > 0 Then
              i = i + 1
              ReDim v(1 To i)
              v(i) = pasteRowIndex
              End If
    
          Sheets("Demand").Rows(r).Copy Sheets("Data").Rows(pasteRowIndex)
         pasteRowIndex = pasteRowIndex + 1
        End If
     Next r
    

    【讨论】:

    • 感谢修复!
    【解决方案2】:

    像这样……

    For r = 1 To LastRow
        If Not IsError(.Cells(r, "L").Value) Then
            If LCase(.Cells(r, "L").Value) Like "record*" Then
                If UBound(Split(.Cells(r, "L"), ",")) > 0 Then
                    i = i + 1
                    ReDim v(1 To i)
                    v(i) = pasteRowIndex
                End If
    
                Sheets("Demand").Rows(r).Copy Sheets("Data").Rows(pasteRowIndex)
                pasteRowIndex = pasteRowIndex + 1
            End If
        End If
    Next r
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多