Excel VBA .find 命令对我不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26184489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:45:33  来源:igfitidea点击:

Excel VBA .find command not working for me

excelexcel-vbafindvba

提问by Dustin Wehri

I have an Excel 2013 sheet that has lots of tabs. Each tab holds license keys for a different piece of software that our company has. The License is then tied to the Computer it is installed on. I am writing a macro that lists all the tabs that a a search string is found on. If i leave the inputbox empty, it will list all the tabs. But if i type in something to search for it does nothing but show the end of macro message.

我有一个 Excel 2013 工作表,其中有很多选项卡。每个选项卡都包含我们公司拥有的不同软件的许可证密钥。然后,许可证将绑定到安装它的计算机。我正在编写一个宏,其中列出了找到搜索字符串的所有选项卡。如果我将输入框留空,它将列出所有选项卡。但是,如果我输入要搜索的内容,它只会显示宏消息的结尾。

Sub TempMacro()
    Dim ws As Worksheet
    SearchListRow = 3
    Dim rng As Range
    Dim FindSting As String

    FindString = InputBox("Enter a Search value")

    For Each ws In ThisWorkbook.Sheets

        'Set Search Function
        Set rng = Cells.Find(What:=FindString, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

        If Not rng Is Nothing Then
            'Record tab name where string was found
            Sheets("LISTS").Range("O" & SearchListRow) = ws.Name
            'incriment row
            SearchListRow = SearchListRow + 1
        Else
            'MsgBox ("Nothing found in " & wb.Name)
        End If
    Next ws
    MsgBox ("End Macro")
End Sub

回答by Siddharth Rout

That is because your cells object is not fully qualified. What you are doing is searching the activesheet only. You need to add Wsbefore Cellsso that it searches in those sheets

那是因为您的单元格对象不完全合格。您正在做的只是搜索活动表。您需要在Ws之前添加,Cells以便在这些工作表中搜索

Try this

尝试这个

Set rng = Ws.Cells.Find(What:=FindString......

also LookAt:=xlWholedoes an exact match. For partial matches, you will have to use xlPart

LookAt:=xlWhole完全匹配。对于部分匹配,您将不得不使用xlPart