Excel VBA代码来查找某个单元格并删除其周围的部分

我在Excel中写一个macros。 部分代码会查找其中包含“附加打包”的单元,然后删除该单元周围的一组单元的内容。

以下是目前实现的代码:

Cells.Find("Attached Packaging").Activate ActiveCell.Resize(2, 4).Select Selection.Clear ActiveCell.Offset(1, -1).Select Selection.Clear 

我现在的问题是,我出乎意料地拥有多个“附加包装”单元,现在也必须删除。

所以,总结一下:我需要修改这个代码,以便find电子表格中的所有“附加打包”单元格,并删除它们周围的组。

 Sub clear() Dim ws As Worksheet Dim search As String Dim f As Variant Dim fRow As Long Dim fCol As Long search = "Attached Packaging" Set ws = ThisWorkbook.Worksheets("Sheet4") 'change sheet as needed With ws.Range("A1:AA1000") 'change range as needed Set f = .Find(search, LookIn:=xlValues) If Not f Is Nothing Then Do fRow = f.Row fCol = f.Column ws.Range(Cells(fRow, fCol), Cells(fRow + 1, fCol + 3)).clear ws.Cells(fRow + 1, fCol - 1).clear Set f = .FindNext(f) Loop While Not f Is Nothing End If End With End Sub 
 Sub clearCells() Dim ws As Worksheet Dim lastrow As Long, currow As Long Dim critvalue As String Set ws = Sheets("Sheet1") ' Change A to a row with data in it lastrow = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row - 1 'Change 2 to the first row to check For currow = 2 To lastrow critvalue = "Attached Packaging" ' Change A to the row you are looking in If ws.Range("A" & currow).Value = critvalue Then ' Use the currow to select the row and then create an offset ws.Range("A" & currow).offset("B" & currow - 1).clear ws.Range("A" & currow).offset("B" & currow + 1).clear End If Next currow End Sub 

在我所评论的地方进行所需的更改。 它应该工作。