【发布时间】:2015-08-25 16:22:07
【问题描述】:
我已经找到了解决方案,但是代码太长了。然后我决定搜索一种将我想要查找并突出显示的所有单词插入单个查找方法的方法。我遇到了一些使用数组的想法,并使用这 3 个代码来编写我的(this、this 和 this),但我是 VBA 的新用户,所以我的最终代码有问题,它仅突出显示数组的最后一个字符串。我认为问题出在逻辑上,但我不知道 VBA 的基础知识,所以我不知道如何纠正它。
我的实际代码:
Sub Sample()
Dim fnd As String
Dim MyAr
Dim i As Long
Dim rng As Range, FoundCell As Range, LastCell As Range, myRange As Range
Set myRange = ActiveSheet.UsedRange
Set LastCell = myRange.Cells(myRange.Cells.Count)
fnd = "hugo/vw/osnabrück"
MyAr = Split(fnd, "/")
For i = LBound(MyAr) To UBound(MyAr)
Set FoundCell = myRange.Find(what:=MyAr(i), after:=LastCell)
If Not FoundCell Is Nothing Then
FirstFound = FoundCell.Address
End If
Set rng = FoundCell
Do Until FoundCell Is Nothing
Set FoundCell = myRange.FindNext(after:=FoundCell)
Set rng = Union(rng, FoundCell)
If FoundCell.Address = FirstFound Then Exit Do
Loop
Next i
If Not rng Is Nothing Then
rng.EntireRow.Interior.ColorIndex = 3
End If
End Sub
例如,使用此代码,我可以找到并突出显示所有“Osnabrück”,但它不会突出显示任何 Hugo 或 VW。
【问题讨论】:
标签: vba excel for-loop excel-2010