[英]Find all offsetCells after specific key word?
这是我的代码,但在包含 the.FindNext 表达式的行出现运行时错误 438。 问题出在哪儿?
Dim bottomCell As Range
Dim offsetCell As Range
With Sheets("C7BB2HD3IINA_NRM_X302")
Set bottomCell = .Cells.Find(what:="KENNFELD")
Set offsetCell = bottomCell.Offset(0, 1)
Set offsetCell = .FindNext(offsetCells)
End With
使用 Find/FindNext 非常复杂,您应该将其拆分为一个单独的 function,它只返回匹配项。 这样您就可以专注于主要逻辑,而不是在Find
过程中纠缠不清。
尝试这个:
Sub tester()
Dim col As Collection, c
Set col = FindAll(ThisWorkbook.Worksheets("C7BB2HD3IINA_NRM_X302").Cells, _
"KENNFELD", xlWhole) 'or xlPart
For Each c In col 'loop over matches
MsgBox c.Offset(0, 1).Value
Next d
End Sub
'Find all matches for `val` in `rng` and return as a Collection of cells
Public Function FindAll(rng As Range, val As String, matchType As XlLookAt) As Collection
Dim rv As New Collection, f As Range
Dim addr As String
Set f = rng.Find(what:=val, after:=rng.Cells(rng.Cells.Count), _
LookIn:=xlValues, LookAt:=matchType, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If Not f Is Nothing Then addr = f.Address()
Do Until f Is Nothing
rv.Add f
Set f = rng.FindNext(after:=f)
If f.Address() = addr Then Exit Do
Loop
Set FindAll = rv
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.