遍历工作表

时间:2019-07-16 08:05:52

标签: excel vba

我正在尝试在所有工作表中寻找某些值。但是,下面的代码只能在第一个工作表上执行。似乎活动表无法更改,请在此问题上帮助我。

Sub Search()

Dim sRange As Range, Rng As Range
Dim Row1 As Integer, Row2 As Integer
Dim FindString As String
Dim WS As Worksheet

Row1 = 13
Row2 = 2

Application.ScreenUpdating = False
FindString = Sheets("Welcome").Range("N10").Value
For Each WS In ThisWorkbook.Worksheets
    If WS.Name <> "Welcome" Then
        WS.Activate
        LastRow2 = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row

        While Row2 < LastRow2
            Set sRange = ActiveSheet.Range(Cells(Row2, 2), Cells(Row2, 9))
            With sRange
                Set Rng = .Find(What:=FindString, After:=.Cells(1), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
                If Not Rng Is Nothing Then
                    Sheets("Welcome").Range("N" & Row1).Value = WS.Name
                    Sheets("Welcome").Range("O" & Row1).Value = Rng.Row
                    Row1 = Row1 + 1
                End If
            End With
            Row2 = Row2 + 1
        Wend

    End If
Next WS
Sheets("Welcome").Activate
Application.ScreenUpdating = True

End Sub

0 个答案:

没有答案