Search 在Excel 2010中搜索并突出显示行
我正在尝试创建一个宏快捷方式来搜索Excel文件中的单词,该文件包含大量的行和列。是否有任何方法可以搜索我想要的单词,然后让程序为我突出显示整行 例如,当我搜索“b”时,它将高亮显示整行,如下所示: 试试这个:Search 在Excel 2010中搜索并突出显示行,search,excel-2010,conditional-formatting,Search,Excel 2010,Conditional Formatting,我正在尝试创建一个宏快捷方式来搜索Excel文件中的单词,该文件包含大量的行和列。是否有任何方法可以搜索我想要的单词,然后让程序为我突出显示整行 例如,当我搜索“b”时,它将高亮显示整行,如下所示: 试试这个: 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
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
这两种代码都符合您的要求。它搜索当前选定的范围。
第二个代码在搜索大型选择时速度更快。
而且两者都只查找精确匹配。运行时出错,我将arr()和搜索项都更改为字符串,调试时,arr=rng.Value为空。为什么会这样?谢谢