简体   繁体   中英

Excel VBA macro finds and deletes only some of the needed cells

I'd like to create a really simple VBA macro for Excel which looks within a range of 9 columns and variable number of rows and finds cells containing specific words, then deletes their contents. And I have this code currently:

Sub FindAllAndDelete()
'
'   
    Dim SearchRange As Range

    Range("B58:J58").Select
    Range(Selection, Selection.End(xlDown)).Select
    Set SearchRange = Range(Selection, Selection.End(xlDown))

    Do While Not IsEmpty(SearchRange)
      Set c1 = SearchRange.Find("Missing")
        c1.ClearContents
      Set c2 = SearchRange.Find("NR")
        c2.ClearContents
      Set c3 = SearchRange.Find("NO")
        c3.ClearContents
    Loop

End Sub

which seems to find and delete the contents of only some of those cells, not all of them. Could you tell me why this happens and maybe give me a hint how to fix it?

Basically find will give you only the first instance of the item that you are looking you. You will have to use findnext as given below

Set SearchRange = Range("B58:J158")
  Set c = SearchRange.Find("NO")
  If Not c Is Nothing Then
    Do
        c.ClearContents
        Set c = SearchRange.FindNext(c)
    Loop While Not c Is Nothing
  End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM