【问题标题】:Excel VBA Loop find x and remove row - errorExcel VBA循环查找x并删除行 - 错误
【发布时间】:2020-12-03 00:04:43
【问题描述】:

我使用记录宏创建了一些代码,然后将其放入循环中。它可以工作,但是 find 函数中有一个错误,导致它只能工作一次。我试图对错误做一些事情,但我没有任何运气让它循环。我在这里和那里看了几天,但我不知所措。希望您能够帮助我。非常感谢。

i = 1
On Error GoTo notfound
 Do While Sheet1.Cells(i, 1) <> "" 
   
 Columns("J:J").Select
    Selection.Find(What:="x", After:=ActiveCell, LookIn:=xlFormulas, _
  LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
  MatchCase:=False, SearchFormat:=False).Activate
        
        Rows(ActiveCell.Row).EntireRow.Delete

notfound: msgbox "Finished"
GoTo notfound
Exit Sub
   
   i = i + 1
   
   Loop

【问题讨论】:

  • 你能描述一下你想让代码做什么吗?

标签: excel vba loops find


【解决方案1】:

我已经更正、完成、格式化和注释了您的代码。这应该会让你离你想要做的事情更近一步。

Private Sub Sample()

    Dim Crit        As Variant      ' the criterium to look for
    Dim Fnd         As Range        ' the cell to find
    Dim i           As Long
    
    ' never create an error handler if you don't know which error to exect
'    On Error GoTo notfound
    i = 1
    ' the cell can't be "" only its value can do thjat
    Do While Sheet1.Cells(i, 1).Value <> ""
        Crit = "x"
'        Columns("J:J").Select
        ' don't Select anything, address cells or ranges instead
        Set Fnd = Columns("J:J").Find(What:=Crit, _
                                      After:=ActiveCell, _
                                      LookIn:=xlFormulas, _
                                      LookAt:=xlPart, _
                                      SearchOrder:=xlByColumns, _
                                      SearchDirection:=xlNext, _
                                      MatchCase:=False, _
                                      SearchFormat:=False)
        ' where is the 'Activecell'?
        '   a) it doesn't change while this loop is running
        '      but it might get deleted by this loop's action
        '   b) it must be in the range you are searching
        '      that's why your code will fail most of the time.
        
        ' don't Activate anything. Instead address the object you want to deal with.
        If Fnd Is Nothing Then
            MsgBox "I didn't find """ & Crit & """"
        Else
            Sheet1.Rows(Fnd.Row).EntireRow.Delete
        End If
        i = i + 1
    Loop
End Sub

只要 A 列中有值,此代码就会在 J 列中查找“x”并删除找到它的行。很难想象 A 列中的条目数与 J 列中的“x”数之间的关系,但希望这不是你的问题。相反,您明显的问题是您要在其中开始搜索的单元格。绝对不是ActiveCell,但可能是Cells(1, "J")。你也可以省略这条指令,VBA会在J1之后开始搜索。

你想LookIn 公式。如果 J 列中有公式,Formula 将不同于 Value。您不妨搜索xlValues

【讨论】:

  • 感谢 Variatus 的尝试。我尝试了您发送的代码并彻底检查了 cmets。不幸的是,它给了我查找函数的类型不匹配错误。我会继续弄乱它,看看我能不能让它工作。至于信息,有 30 列,如果 J 列中有“X”,则整个行都不需要数据。我真的可以引用任何列来获得一些东西,但第一列总是会的。只是想补充一点,J 列中永远不会有公式。其中的“x”字面意思是取消。
  • 更新:Variatus,我通过从 find 函数中删除所有内容来让它工作,但是“Crit”我猜它不喜欢其他一些标准。我不知道那是什么,但好吧。再次感谢。现在我只需要弄清楚如何关闭消息框。我点击关闭,它再次打开。我认为它在某些方面正在经历循环。单击确定需要 16 次才能关闭。我会继续努力的。
  • 我修复了它没有关闭。我只是在消息框后添加了“退出执行”。代码现在可以按我的意愿工作。再次感谢。
  • Find 函数由于 After 属性而不起作用。 ActiveCell 是错误的。我建议删除该属性。只需删除该行。每次Find 函数没有返回结果时,都会显示 MsgBox。如果您的循环循环 16 次,它可能会显示 16 次。您在代码的那个点采取适当的行动是正确的。但是,没有找到 Crit 的原因可能是因为您查看的是 xlFormulas 而不是 xlValues。如果 Crit 是每个循环退出时的不同值,则可能不是要采取的适当操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 2015-08-08
  • 2017-09-13
  • 1970-01-01
相关资源
最近更新 更多