如何在多个Wksheets中同时搜索字符串?

时间:2018-09-18 11:58:33

标签: excel vba excel-vba

我大约有30张纸希望同时运行此代码。我想在所有工作表中找到“ ABC”并删除其旁边的单元格的值。

我的错误来自:Set rSearch = .**range**("A1", .range("A" & rows.count).end(x1up))

当我在“ With”语句旁边指定“ Sheet1”时,它可以工作,但是我希望此代码在我的所有工作表上运行。

Sub soek()

    Dim rSearch As Range
    Dim rFound As Range
    Dim sign12 As String
    Dim sheetsarray As Sheets
    Set sheetsarray = ActiveWorkbook.Sheets(Array("sheet1", "sheet2", "sheet3"))


    sign12 = "ABC"


    With sheetsarray
        Set rSearch = .Range("A1", .Range("A" & Rows.Count).End(xlUp))

        Set rFound = rSearch.Find(What:=sign12, LookIn:=xlValues)

        If rFound Is Nothing Then

        Else
            rFound.Offset(0, 1).ClearContents

        End If
    End With

End Sub

这个问题很像:How to search for a string in all sheets of an Excel workbook?

但是,我认为,比起上面链接中的代码,了解如何使代码在读取我的代码的其他工作表上运行要容易得多。

1 个答案:

答案 0 :(得分:1)

尝试一下(上面评论的汇编;-)

Sub soek()

Dim rSearch As Range
Dim rFound As Range
Dim sign12 As String
Dim oWB As Workbook
Dim oWS As Worksheet

Set oWB = ThisWorkbook

sign12 = "ABC"

For Each oWS In oWB.Sheets

With oWS
    Set rSearch = .Range("A1", .Range("A" & Rows.Count).End(xlUp))

    Set rFound = rSearch.Find(What:=sign12, LookIn:=xlValues)

    If rFound Is Nothing Then

    Else
        rFound.Offset(0, 1).ClearContents

    End If
End With

Next oWS

End Sub