【发布时间】:2023-02-02 05:25:40
【问题描述】:
我的代码就是这样做的。它在我当前的工作表中搜索单词:“KENNFELD”。然后它将变量标签设置为“KENNFELD”右侧的单元格。现在我想在我的整个工作簿中找到变量标签的匹配项,不包括我当前所在的那个,因为那是我首先得到它们的地方。
问题是,这适用于找到的第一个标签,但不适用于其他标签,而且我知道必须再匹配 6 个。我相信我的问题在循环中,但我找不到它。有人有想法吗?
Dim helpc As Range
Dim label As Range
Dim firstAddress As String
Dim foundCell As Range
With Sheets("C7BB2HD3IINA_NRM_X302")
Set helpc = .Cells.Find(what:="KENNFELD", MatchCase:=True)
Set label = helpc.Offset(0, 1) ' assign the value of the cell to label
If Not helpc Is Nothing Then
firstAddress = helpc.Address
Do
For Each ws In ThisWorkbook.Sheets
If ws.Name <> "C7BB2HD3IINA_NRM_X302" Then
Set foundCell = ws.Cells.Find(what:=label.Value, LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=True)
If Not foundCell Is Nothing Then
MsgBox "Label " & label.Value & " found on sheet " & ws.Name
End If
End If
Next ws
Set helpc = .Cells.FindNext(helpc)
Loop While Not helpc Is Nothing And helpc.Address <> firstAddress
End If
End With
【问题讨论】:
-
我认为
Set helpc = .Cells.FindNext(helpc)应该是Set foundCell = ws.Cells.FindNext(foundCell)。 -
一般来说,我认为你混淆了两个查找,所以你的循环应该基于
foundCell而不是helpc,即Loop While行也需要修改。 -
您不知道
"KENNFIELD"的行或列和/或其他工作表中的标签吗?找到每个标签后,您不想做一些更有用的事情吗?你能分享标签是什么吗,即它是一个字符串吗?为什么必须完全匹配(区分大小写)?它可以出现在不同的小写和大写字符中吗?你有隐藏的行或列吗?是否过滤了其他工作表?
标签: excel vba loops for-loop autofilter