简体   繁体   English

使用VBA在EXCEL中搜索特定单词

[英]Search a specific word in EXCEL using VBA

Im a beginner with Excel VBA my question is if I have string in a cell like "MyNameIsHammadAndImFromIreland" . 我是Excel VBA的初学者,我的问题是我是否在像"MyNameIsHammadAndImFromIreland"这样的单元格中有字符串。 All cells have strings/sentences but there is no spaces. 所有单元格都有字符串/句子,但没有空格。 How can I find if the cell includes the word "Ireland" ? 如何查找单元格中是否包含"Ireland"一词?

You don't need VBA, just use a formula: 您不需要VBA,只需使用以下公式即可:

=IF(LEN(SUBSTITUTE(A1,"Ireland",""))<LEN(A1),"Word Found","Word NOT found")  

If you really want VBA then: 如果您真的想要VBA,则:

hasWord = InStr(Range("A1").Value, "Ireland") > 0 '// returns TRUE or FALSE

The following VBA code will iterate through the used cells in a given worksheet and find any that match the given criteria. 以下VBA代码将遍历给定工作表中使用的单元格,并找到与给定条件匹配的任何单元格。 At the end, a message box is displayed showing a list of cells that contain the search term. 最后,显示一个消息框,显示包含搜索词的单元格列表。

This functionality is equivalent to using the 'Find All' option you can choose when you use the normal find functionality available by pressing CTRL + F . 此功能等效于使用“全部查找”选项,当您使用常规查找功能时,可以通过按CTRL + F进行选择

Option Explicit

Private Sub Find()

        Dim rngResult As Range
        Dim strToFind As String

        'Set to your desired string to find
        strToFind = "Ireland"

        'If the string you are searching for is located in
        'the worksheet somewhere, you can set the value
        'like this:  strToFind = Worksheets("Sheet1").Range("A1").Value
        'This assumes your search term is in cell A1 of the
        'Sheet1 worksheet.

        'Look in the used range of a given worksheet
        'Change Sheet1 to match your worksheet name
        With Worksheets("Sheet1").UsedRange

                'Find the first cell that contains the search term
                Set rngResult = .Find(What:=strToFind, LookAt:=xlPart)

                'If it is found, grab the cell address of where the
                'search term can be found
                If Not rngResult Is Nothing Then
                        Dim firstAddress As String, result As String
                        firstAddress = rngResult.Address

                'Loop through the rest of the cells until returning
                'to the first cell that we had a match in.
                Do
                        'Record the cell address of the match
                        'to the result string
                        result = result & rngResult.Address & ","

                        'Go to next cell containing the search term
                        Set rngResult = .FindNext(rngResult)

                'Exit the loop when we reach the starting point
                Loop While rngResult.Address <> firstAddress

                'Output the list of cells that contain the string to find
                MsgBox "Found """ & strToFind & """ in cell(s): " & result

                End If
        End With

End Sub

Be sure to set strToFind to your desired string, and change Sheet1 to match the name of whichever sheet you want to search over. 确保将strToFind设置为所需的字符串,并更改Sheet1以匹配要搜索的工作表的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM