0

我正在尝试创建一个宏快捷方式来在我的 Excel 文件中搜索一个单词,该文件由大量的行和列组成。有什么方法可以搜索我想要的单词,然后让程序为我突出显示整行?

例子

例如,当我搜索“b”时,它会像这样突出显示整行:

在此处输入图像描述

4

1 回答 1

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

两个代码都可以满足您的要求。
它搜索当前选定的范围。
搜索大型选择时,第二个代码更快。
两者都只找到完全匹配。

于 2013-10-14T08:55:50.900 回答