从其他工作簿中搜索

问题描述 投票:0回答:1

我有一本工作簿,其中的数据分几页(一个品牌)。我需要从其他工作簿中搜索并获得所有结果。

此代码有效,但仅当在同一工作簿中搜索时。

有人知道我该如何修改我的代码吗?

Sub Search_With_Multiple_Results()

Dim Results As String
Dim Sheet As Worksheet
Dim Found
Dim FirstAddress
Dim r

r = 5

 For Each Sheet In ThisWorkbook.Worksheets
    Set Found = Nothing
    With Sheet.UsedRange
        Set Found = .Cells.Find(What:="Value searched", LookIn:=xlValues, LookAt:=xlPart,  SearchOrder:=xlRows, SearchDirection:=xlNext, MatchCase:=False)
        If Not Found Is Nothing Then
            FirstAddress = Found.Address
            Do
                If Results = vbNullString Then
                    r = r + 1: Results = "Worksheet(" & Sheet.Index & ").Range(" & Chr(34) & 
Found.Address & Chr(34) & ")"
                    Cells(r, 7).Value = Results
                Else
                    Results = Results & "|" & "Worksheet(" & Sheet.Index & ").Range(" & Chr(34) &  Found.Address & Chr(34) & ")"
                        r = r + 1: Results = "Worksheet(" & Sheet.Index & ").Range(" & Chr(34) & 
Found.Address & Chr(34) & ")"
                        Cells(r, 7).Value = Results
                End If
                Set Found = .FindNext(Found)
            Loop While Not Found Is Nothing And Found.Address <> FirstAddress
        End If
    End With
Next
    If FirstAddress = Empty Then
        MsgBox ("Not found")
    End If
End Sub
excel vba search
1个回答
0
投票
For Each Sheet In ThisWorkbook.Worksheets

For Each Sheet In Workbooks("All Brands.xlsx").Worksheets
© www.soinside.com 2019 - 2024. All rights reserved.