循环到另一个表之前如何重置FIND函数结果?

我想问问你是否可以帮助下面的代码。 在我的工作簿中的每一张纸上都有相同types的表,但是在每张纸上,表具有不同的位置和值。 我需要浏览所有表格,在每张表格上search表格值,然后使用值进行一些其他操作。 我使用查找function来确定表头和随后的表范围。 Find函数不能正常工作,因为它为每个其他表单保留了第一个表格的“标题”单元格的地址。 有什么方法可以在循环到另一个表之前重置find的标头地址值? 先谢谢你。

Sub FindInDynamicRanges() Dim wb1 As Workbook Dim ws As Worksheet Dim FoundCell, FoundTab, TabEntries As Excel.Range Dim FirstAddr As String Dim FirstRow, LastRow As Long Set wb1 = ThisWorkbook 'Find all occurences of any table value on all sheets in dynamic ranges For Each ws In wb1.Worksheets Set ws = ActiveSheet 'Find "Header" cell Set FoundCell = ws.Columns(2).Find(What:="Header", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _ SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) MsgBox FoundCell.Address 'Set number of first entry row and last entry row FirstRow = FoundCell.Row + 1 LastRow = ws.Cells.Find(What:="*", After:=Range("A1"), LookAt:=xlPart, LookIn:=xlValues, SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, MatchCase:=False).Row ws.Range("B" & FirstRow & ":B" & LastRow).Name = "TabEntries" MsgBox Range("TabEntries").Address With ws.Range("TabEntries") Set LastCell = .Cells(.Cells.Count) End With Set FoundTab = ws.Range("TabEntries").Find(What:="*", After:=LastCell, LookIn:=xlValues, LookAt:=xlWhole, _ SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) If Not FoundTab Is Nothing Then FirstAddr = FoundTab.Address End If Do Until FoundTab Is Nothing 'do some staff with found values Set FoundTab = ws.Range("TabEntries").FindNext(After:=FoundTab) If FoundTab.Address = FirstAddr Then Exit Do End If Loop Next ws End Sub 

因为它为每隔一张纸保留第一张纸的“标题”单元格的地址。

那是因为你告诉它…

 For Each ws In wb1.Worksheets Set ws = ActiveSheet 

你不需要Set ws = ActiveSheet

当你说For Each wsws会自动初始化。 所以只要删除第二行。