繁体   English   中英

VBA Excel 在列中查找字符串并偏移删除并重复

[英]VBA Excel Find string in column and offset delete and repeat

我有一个工作代码可以在特定工作表的列中查找特定字符串,偏移并清除特定单元格的内容。 但是,它只会清除第一次出现的搜索,我希望代码能处理所有出现的情况。 有人可以帮我围绕这段代码包装一个 Loop 或 FindNext 吗,因为我做不到。 请在下面查看我已有的代码。 谢谢

Dim SearchValue6 As String 'located B9 
Dim Action6 As Range 'clear  
SearchValue6 = Workbooks.Open("C:\Users\.......xlsm").Worksheets("Sheet1").Range("B9").Value
    
On Error Resume Next

Worksheets(2).Columns("A:A").Select
Set Action6 = Selection.Find(What:=SearchValue6, After:=ActiveCell, LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

If Action6 Is Nothing Then
    'MsgBox "No clearings made in " & ActiveWorkbook.Name

Else
Action6.Activate
ActiveCell.Offset(0, 1).Select
ActiveCell.ClearContents

End If

请尝试使用下一个更新的代码并发送一些反馈:

Sub FindMultipleTimes()
   Dim SearchValue6 As String 'located B9
   Dim Action6 As Range 'clear
   SearchValue6 = Workbooks.Open("C:\Users\.......xlsm").Worksheets("Sheet1").Range("B9").Value
    
   Dim ws As Worksheet: Set ws = Worksheets(2)
   Dim firstAddress As String
   Set Action6 = ws.Columns("A:A").Find(What:=SearchValue6, After:=ws.Range("A1"), LookIn:=xlFormulas2, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)

   If Not Action6 Is Nothing Then
            firstAddress = Action6.address
            Do
                    Action6.Offset(0, 1).ClearContents
                    Set Action6 = ws.Columns("A:A").FindNext(Action6) 'find the next occurrence
            Loop While Action6.address <> firstAddress
   Else
        MsgBox SearchValue6 & " could not be found in column ""A:A"" of sheet " & ws.name
   End If
End Sub

我只调整了你的代码,但你想让提取SearchValue6值所需的工作簿打开吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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