我正在尝试创建一个宏快捷方式来搜索Excel文件中的单词,该单词包含大量的行和列。有什么方法可以搜索我想要的单词然后让程序为我突出显示整行吗?
例如,当我搜索" b"时,它会突出显示整行:
答案 0 :(得分:0)
试试这个:
Sub test()
Dim rng, cel As Range
Dim search_item
Set rng = Selection
search_item = InputBox("Find what?", "Test")
For Each cel In rng
If cel.Value = search_item Then
cel.EntireRow.Select
Exit For
End If
Next cel
End Sub
希望这能让你开始。
<强>更新强> 你提到你搜索大行 不妨用这个:
Sub test()
Dim rng As Range
Dim arr() As Variant 'i do not know your data type
Dim search_item As Variant 'i do not know your data type, change to your liking
Dim lrow, lcol As Long
Dim found As Boolean
Set rng = Selection
lrow = rng.Rows.Count
lcol = rng.Columns.Count
ReDim arr(1 To lrow, 1 To lcol)
arr = rng.Value
search_item = InputBox("Find what?", "Test")
For i = 1 To lrow
For j = 1 To lcol
If arr(i, j) = search_item Then
rng.Cells(i, j).EntireRow.Select
found = True
Exit For
End If
Next j
If found Then Exit For
Next i
End Sub
这两个代码都能满足您的需求 它搜索当前选择的范围 搜索大量选择时,第二个代码更快 两者都发现只有完全匹配。