Найти часть текста в ячейке excel vba

I want to check a range of cells for a certain piece of text. This text is always in my document, except it’s cell is variable (column is always B). So I check a range from 1:75 whether any cells contain a piece of text, but it doesn’t seem to work.

Dim FoundRange As Range
Set FoundRange = Cells.Find("5/7 binnen 4h")
Range("I" & EmptyCell + 2).Value = ... (value of cell I on same row as B)

Cell I’m looking for always contains this text Onderhoud 5/7 binnen 4h but its position can vary, that’s why I just need to check whether it contains any of it. When I find that cell, I need the value I on the same row.

Any suggestions are welcome!

Community's user avatar

asked Feb 23, 2012 at 16:13

CustomX's user avatar

2

Could you not just search for the substring?

Sheet1.Cells.Find("string to find")

Will return the a range containing the string (or nothing if the string can’t be found.

For example

Public Sub Macro1()
Dim FoundRange As Range

Set FoundRange = Sheet1.Cells.Find("5/7 binnen 4h")

' display the cell address to the user
MsgBox FoundRange.Address


' put the found value in column i in the same row as the found text in a known location ($C$1 in this case)
Sheet1.Range("$C$1").Value = Sheet1.Cells(FoundRange.Row, 9).Value

' put the found value in four columns to the right in the same row as the found text in a known location ($C$1 in this case)
Sheet1.Range("$C$2").Value = FoundRange.Offset(0, 4).Value

End Sub

answered Feb 23, 2012 at 16:26

Steve Homer's user avatar

Steve HomerSteve Homer

3,8272 gold badges21 silver badges40 bronze badges

9

This is too much to fit into a comment, so posting it as an answer…

You should be careful when using Find() with just a single argument: if you’ve previously used Find() in your code and (eg) specified an argument lookat:=xlWhole then you may not get the results you expect, particularly if you’re looking for a substring of a cell’s value. Settings passed to Find() are persistent: if you don’t specify an argument then it may be carried over from the previous use.

As an example (working with a sheet containing the text «hello tom» in B4:

Sub Tester()

    Dim f

    Set f = ActiveSheet.Cells.Find(what:="tom", lookat:=xlPart)
    Report f

    Set f = ActiveSheet.Cells.Find(what:="tom", lookat:=xlWhole)
    Report f

    Set f = ActiveSheet.Cells.Find("tom")
    Report f

End Sub

Sub Report(f)
    If Not f Is Nothing Then
        Debug.Print f.Address
    Else
        Debug.Print "not found"
    End If
End Sub

Running this gives:

$B$4
not found
not found

I seem to recall this is also the case if you’ve used Find() «manually» and then use it in code later in the same session (didn’t test though).

answered Feb 23, 2012 at 17:24

Tim Williams's user avatar

Tim WilliamsTim Williams

150k8 gold badges96 silver badges124 bronze badges

2

Метод Find объекта Range для поиска ячейки по ее данным в VBA Excel. Синтаксис и компоненты. Знаки подстановки для поисковой фразы. Простые примеры.

Метод Find объекта Range предназначен для поиска ячейки и сведений о ней в заданном диапазоне по ее значению, формуле и примечанию. Чаще всего этот метод используется для поиска в таблице ячейки по слову, части слова или фразе, входящей в ее значение.

Синтаксис метода Range.Find

Expression.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)

Expression – это переменная или выражение, возвращающее объект Range, в котором будет осуществляться поиск.

В скобках перечислены параметры метода, среди них только What является обязательным.

Метод Range.Find возвращает объект Range, представляющий из себя первую ячейку, в которой найдена поисковая фраза (параметр What). Если совпадение не найдено, возвращается значение Nothing.

Если необходимо найти следующие ячейки, содержащие поисковую фразу, используется метод Range.FindNext.

Параметры метода Range.Find

Наименование Описание
Обязательный параметр
What Данные для поиска, которые могут быть представлены строкой или другим типом данных Excel. Тип данных параметра — Variant.
Необязательные параметры
After Ячейка, после которой следует начать поиск.
LookIn Уточняет область поиска. Список констант xlFindLookIn:

  • xlValues (-4163) – значения;
  • xlComments (-4144) – примечания*;
  • xlNotes (-4144) – примечания*;
  • xlFormulas (-4123) – формулы.
LookAt Поиск частичного или полного совпадения. Список констант xlLookAt:

  • xlWhole (1) – полное совпадение;
  • xlPart (2) – частичное совпадение.
SearchOrder Определяет способ поиска. Список констант xlSearchOrder:

  • xlByRows (1) – поиск по строкам;
  • xlByColumns (2) – поиск по столбцам.
SearchDirection Определяет направление поиска. Список констант xlSearchDirection:

  • xlNext (1) – поиск вперед;
  • xlPrevious (2) – поиск назад.
MatchCase Определяет учет регистра:

  • False (0) – поиск без учета регистра (по умолчанию);
  • True (1) – поиск с учетом регистра.
MatchByte Условия поиска при использовании двухбайтовых кодировок:

  • False (0) – двухбайтовый символ может соответствовать однобайтовому символу;
  • True (1) – двухбайтовый символ должен соответствовать только двухбайтовому символу.
SearchFormat Формат поиска – используется вместе со свойством Application.FindFormat.

* Примечания имеют две константы с одним значением. Проверяется очень просто: MsgBox xlComments и MsgBox xlNotes.

В справке Microsoft тип данных всех параметров, кроме SearchDirection, указан как Variant.

Знаки подстановки для поисковой фразы

Условные знаки в шаблоне поисковой фразы:

  • ? – знак вопроса обозначает любой отдельный символ;
  • * – звездочка обозначает любое количество любых символов, в том числе ноль символов;
  • ~ – тильда ставится перед ?, * и ~, чтобы они обозначали сами себя (например, чтобы тильда в шаблоне обозначала сама себя, записать ее нужно дважды: ~~).

Простые примеры

При использовании метода Range.Find в VBA Excel необходимо учитывать следующие нюансы:

  1. Так как этот метод возвращает объект Range (в виде одной ячейки), присвоить его можно только объектной переменной, объявленной как Variant, Object или Range, при помощи оператора Set.
  2. Если поисковая фраза в заданном диапазоне найдена не будет, метод Range.Find возвратит значение Nothing. Обращение к свойствам несуществующей ячейки будет генерировать ошибки. Поэтому, перед использованием результатов поиска, необходимо проверить объектную переменную на содержание в ней значения Nothing.

В примерах используются переменные:

  • myPhrase – переменная для записи поисковой фразы;
  • myCell – переменная, которой присваивается первая найденная ячейка, содержащая поисковую фразу, или значение Nothing, если поисковая фраза не найдена.

Пример 1

Sub primer1()

Dim myPhrase As Variant, myCell As Range

myPhrase = «стакан»

Set myCell = Range(«A1:L30»).Find(myPhrase)

If Not myCell Is Nothing Then

MsgBox «Значение найденной ячейки: « & myCell

MsgBox «Строка найденной ячейки: « & myCell.Row

MsgBox «Столбец найденной ячейки: « & myCell.Column

MsgBox «Адрес найденной ячейки: « & myCell.Address

Else

MsgBox «Искомая фраза не найдена»

End If

End Sub

В этом примере мы присваиваем переменной myPhrase значение для поиска – "стакан". Затем проводим поиск этой фразы в диапазоне "A1:L30" с присвоением результата поиска переменной myCell. Далее проверяем переменную myCell, не содержит ли она значение Nothing, и выводим соответствующие сообщения.

Ознакомьтесь с работой кода VBA в случаях, когда в диапазоне "A1:L30" есть ячейка со строкой, содержащей подстроку "стакан", и когда такой ячейки нет.

Пример 2

Теперь посмотрим, как метод Range.Find отреагирует на поиск числа. В качестве диапазона поиска будем использовать первую строку активного листа Excel.

Sub primer2()

Dim myPhrase As Variant, myCell As Range

myPhrase = 526.15

Set myCell = Rows(1).Find(myPhrase)

If Not myCell Is Nothing Then

MsgBox «Значение найденной ячейки: « & myCell

Else: MsgBox «Искомая фраза не найдена»

End If

End Sub

Несмотря на то, что мы присвоили переменной числовое значение, метод Range.Find найдет ячейку со значением и 526,15, и 129526,15, и 526,15254. То есть, как и в предыдущем примере, поиск идет по подстроке.

Чтобы найти ячейку с точным соответствием значения поисковой фразе, используйте константу xlWhole параметра LookAt:

Set myCell = Rows(1).Find(myPhrase, , , xlWhole)

Аналогично используются и другие необязательные параметры. Количество «лишних» запятых перед необязательным параметром должно соответствовать количеству пропущенных компонентов, предусмотренных синтаксисом метода Range.Find, кроме случаев указания необязательного параметра по имени, например: LookIn:=xlValues. Тогда используется одна запятая, независимо от того, сколько компонентов пропущено.

Пример 3

Допустим, у нас есть многострочная база данных в Excel. В первой колонке находятся даты. Нам необходимо создать отчет за какой-то период. Найти номер начальной строки для обработки можно с помощью следующего кода:

Sub primer3()

Dim myPhrase As Variant, myCell As Range

myPhrase = «01.02.2019»

myPhrase = CDate(myPhrase)

Set myCell = Range(«A:A»).Find(myPhrase)

If Not myCell Is Nothing Then

MsgBox «Номер начальной строки: « & myCell.Row

Else: MsgBox «Даты « & myPhrase & » в таблице нет»

End If

End Sub

Несмотря на то, что в ячейке дата отображается в виде текста, ее значение хранится в ячейке в виде числа. Поэтому текстовый формат необходимо перед поиском преобразовать в формат даты.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Sub Поиск_фрагментов()
     Dim i&, j&, k&, LastRow&, A, B, Z
     LastRow = Cells(Rows.Count, 2).End(xlUp).Row
     A = Range("B1:J" & LastRow).Value
     B = Range("F1:F" & LastRow).Value
     Range("G1:G" & LastRow).Value = "нет"
     For i = 2 To LastRow
        A(i, 1) = Trim$(A(i, 1))
        B(i, 1) = Trim$(B(i, 1))
        A(i, 5) = ""
        A(i, 6) = ""
        A(i, 7) = ""
        A(i, 8) = ""
        A(i, 9) = ""
        If Left(A(i, 1), 1) = "0" Then
            A(i, 5) = format$(Val(A(i, 1)))
        Else
            k = InStr(1, A(i, 1), "-")
            If k > 0 Then
                A(i, 5) = Replace$(A(i, 1), "-", "/")
                A(i, 6) = Replace$(A(i, 1), "-", "")
                Z = Split(A(i, 1), "-")
                A(i, 9) = Z(1)
                If Left(Z(1), 1) = "0" Then
                    A(i, 7) = Z(0) & "-" & format$(Val(Z(1)))
                    A(i, 8) = Z(0) & "/" & format$(Val(Z(1)))
                    A(i, 9) = format$(Val(Z(1)))
               End If
            End If
            k = InStr(1, A(i, 1), "/")
            If k > 0 Then
                Z = Split(A(i, 1), "/")
                A(i, 9) = Z(1)
                If Left(Z(1), 1) = "0" Then
                    A(i, 7) = Z(0) & "/" & format$(Val(Z(1)))
                    A(i, 8) = format$(Val(Z(1)))
                End If
            End If
        End If
        For j = 1 To 9
            If B(i, 1) <> "" And A(i, j) <> "" Then
               k = InStr(1, B(i, 1), A(i, j))
               If k > 0 Then
                   Cells(i, "G") = "да"
                   Cells(i, "F").Characters(k, Len(A(i, j))).Font.color = vbRed
               End If
            End If
        Next
     Next i
End Sub

You will find everything you need to know on the Excel VBA Find function. The Range Find function allows you to find cells within your Excel worksheet (and workbook) that contain a certain text or value. In this post let us explore the many ways in which you can use the Find function.

Looking to search text within VBA strings instead? See the VBA InStr and VBA InStrRev functions

Before we show how to use VBA to search for text within an Excel spreadsheet let us first see how to do it Excel and explore the usually unknown features of the famous CTRL+F combo. See below where you can find it within the Home ribbon and Editing group.Excel Search feature CTRL+F
By clicking the above or simply using the key combo CTRL+F we can enter the Find & Replace modal window.
Excel Find and Replace example
As you notice above Excel easily finds 10 matches for the cells on the left. However there are several more interesting search combinations you can use, including usage of wildcards you can use to get more specific patterns. See some examples below:

Find Matches
some*text
  • some*text
  • some other text
some ?
  • some text
  • some other text
some*e*a
  • somedeal

As you might have already noticed I used 2 types of wildcards above:

  • * – the asterisk symbol represents zero or many of any type of characters. It can be injected between characters to replace either no or any number of characters.
  • ? – the question mark represents at least 1 character.

Now that we have a hand of the basic features of Excel in terms of word search let us move to how to use Excel VBA to find cells containing specific text.

VBA Range Find function

The VBA Find function is in fact a function of the Excel VBA Range object class. See Microsoft documentation for more details. A VBA Range represents any subset of cells within a spreadsheet – it can be a single cell, entire row or a patchwork of different cells and other Ranges. Executing the Find function in fact limits the search to only the cells within its Range object.

Below is the definition of the VBA Range Find function and its parameters:

.Find(What, [After] [LookIn], [LookAt], [SearchOrder], 
     [SearchDirection], [MatchCase], [MatchByte], [SearchFormat])

The Find function returns only the first match within the Range. To search for next items you need to follow it up with the FindNext function.

Parameter Required Description
What Required The value you are searching for
After Optional The cell range from which you start your search from
LookIn Optional What to search in e.g. Formulas, Values or Comments – constants of XlFindLookIn: xlValues, xlFormulas, xlComments, xlCommentsThreaded
LookAt Optional Whether to search in a part of the string in a cell or whether it needs to match the entire cell string – constants of XlLookAt: xlWhole, xlPart
SearchOrder Optional The sequence of the search i.e. whether to search by rows or columns – constants of XlSearchOrder: xlByRows or xlByColumns
SearchDirection Optional Whether to search forward (next) or backwards (previous) – constants of XlSearchDirection: xlNext, xlPrevious
MatchCase Optional Case sensitive or not – True or False
MatchByte Optional Used for double byte languages. True to have double-byte characters match only double-byte characters – True or False
SearchFormat Optional Allow searching by format. See Application.FindFormat – True or False

VBA Find – simple example

We will start with a very simple VBA Range Find function example – searching for a single cell within the entire Excel active spreadsheet:

Dim ws As Worksheet
Set ws = ActiveSheet

Debug.Print ws.Cells.Find("some")

Output:

some text

As you can see it found the first match within the Activesheet (currently open and top spreadsheet) and returned the found value.

VBA Find All

Finding all matches is a little more complicated in VBA than in Excel. We will need to use Do While loop to run via all matches:

Dim searchRange As Range, found As Range, firstFind As Range
    
'Set the search range to the entire worksheet
Set searchRange = ActiveSheet.Cells
    
'Search for the first match
Set found = searchRange.Find("some")
'Save the first found cell to check later whether we have completed the search
Set firstFind = found
    
'Loop through all items using FindNext Range function
Do
   If Not (found Is Nothing) Then
      Debug.Print found.Value
      Set found = searchRange.FindNext(found)
   End If
Loop While Not (found = firstFind)

Output:

some text
some other text
someone
something
somedeal
someones
somerset
someway
somewhat
somewhen

I highlighted above 2 key functions that were used the Range Find Function and the Range FindNext Function. As I mentioned above the Find function will only return the first match. To get next matches you need to run FindNext on the original range. This is I am executing FindNext on the searchRange variable and not the found variable.

Another interesting point to notice is the Do While…loop. Notice I am comparing the found variable to the firstFind variable. This is because when running FindNext it will at some point move to the first match once again and thus never end… it will just keep going in a cirle! Thus the loop is set to end once the FindNext function returns the same first cell.

Find using Wildcards

As mentioned above you can use 2 types of wildcards the asterisk * (zero or more characters) and the question mark ? (at least 1 character) to match slightly more complicates cases.

Dim ws As Worksheet
Set ws = ActiveSheet
   
Debug.Print ws.Cells.Find("some ?")
'Output: some text

Debug.Print ws.Cells.Find("some*w")
'Output: someway

VBA Find with After

To remind the After parameter of the Excel VBA Range Find function defines where you will start your search. It is useful when you don’t want to redefine your range just for the purpose of the search activity. See the example below:

Debug.Print Range("B2:B5").Find("some ?", After:=Range("B3"))

Output:

someone

As you can see below the Find function starts searching for the “some” string just after cell B3 i.e. it will start at B4 where it finds the first matching string.
VBA Find using After parameter

Find After – avoid wrap around

Even if we specify that the VBA Range Find function should start searching for the string after a specified cell, it might wrap around back to the beginning of the Range we specified if it does not find a match when going down. See example below to understand:

Debug.Print Range("B2:B5").Find("some*text", After:=Range("B3"))

Output:

some text

VBA Find using After - wrapping
As you see the search started at B4 however once the search pattern “some*text” was not found until B5 the function resumed search on the remaining cells B2:B3 to find “some text”.

Find After Avoid wrapping using VBA Find

What to do to avoid this wrapping? We can check whether the found text is not within the preceding range using the Application.Intersect function.

If the found cell is before our After cell then we can handle it as such:

 
Set found = Range("B2:B5").Find("some*text", After:=Range("B3"))

If Intersect(Range("B2:B3"), found) Is Nothing Then
    Debug.Print "Found text: " & found.Value
Else
    Debug.Print "Text found is within excluded range"
End If

Output:

Text found is within excluded range

However if the found cell is only After the cell we specified then it will show like this:

 
Set found = Range("B2:B5").Find("some", After:=Range("B3"))

If Intersect(Range("B2:B3"), found) Is Nothing Then
    Debug.Print "Found text: " & found.Value
Else
    Debug.Print "Text found is within excluded range"
End If

Output:

Found text: someone

Find in Values, Formulas, Comments

The VBA Range Find function allows you not only to search within Values (the evalution of your Excel formulas). It can also search instead (or including) within Formulas, Comments and even Excel Threaded Comments.

Let us explore how to use the LookIn parameter to look into specific attributes of a cell.
VBA Range Find using LookIn to search in Values, Formulas, Notes or Threaded Comments
In the code below we will search for the word dog within Values, Formulas, Notes and Threaded Comments (just the first one). We will return the address first. Notice that for Formulas the result was the same – this is because the Value and Formula is the same in this case.

    
Debug.Print Range("A1:D4").Find("dog", LookIn:=xlValues).AddressLocal
'Output: $A$2

Debug.Print Range("A1:D4").Find("dog", LookIn:=xlFormulas).AddressLocal
'Output: $A$2 - as the formula and value for "dog" are the same in this case
Debug.Print Range("A1:D4").Find("This is a dog", LookIn:=xlFormulas).AddressLocal
'Output: $B$2

Debug.Print Range("A1:D4").Find("dog", LookIn:=xlNotes).AddressLocal
'Output: $C$2

Debug.Print Range("A1:D4").Find("dog", LookIn:=xlCommentsThreaded).AddressLocal
'Output: $D$2

The same code but this time returning the actual Value, Formula, Note or Comment:

    
Debug.Print Range("A1:D4").Find("dog", LookIn:=xlValues).Value
'Output: dog

Debug.Print Range("A1:D4").Find("dog", LookIn:=xlFormulas).Formula2Local
'Output: dog
Debug.Print Range("A1:D4").Find("This is a dog", LookIn:=xlFormulas).Formula2Local
'Output: =IF(A2="Dog", "This is a Dog","Other")

Debug.Print Range("A1:D4").Find("dog", LookIn:=xlNotes).NoteText
'Output: This is a note about a dog

Debug.Print Range("A1:D4").Find("dog", LookIn:=xlCommentsThreaded).CommentThreaded.Text
'Output: This is a threaded comment about a dog

Find After – avoid wrap around

Complex patterns for Find

In some cases the pattern you want to find might be more complicated such as e.g. looking for cells with any sequence of numbers, emails, addresses, phone numbers etc. In this case the VBA Range Find function will be too limited. However, there is a solution with the help of so call VBA Regular Expressions. Regular Expressions help define almost any search pattern and are widely used in other programming languages.

If you want to learn more read my VBA Regex Tutorial otherwise a very simple example below.
Find complex text patterns with regular expressions
In below code snippet we would like to find only phone numbers – so we will create a simple expression that finds any sequence of digits.

    
'Define the Regular Expression
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
 
With regex
  'We will look only for sequences of at least 1 digit
  .Pattern = "[0-9]+"
End With
 

'Search in all cells within the Range
Dim r As Range
For Each r In Range("A1:D4")
    If regex.Test(r.Value) Then
        Debug.Print "Found a match: " & r.AddressLocal
    End If
Next r
Found a match: $A$3

Поиск какого-либо значения в ячейках Excel довольно часто встречающаяся задача при программировании какого-либо макроса. Решить ее можно разными способами. Однако, в разных ситуациях использование того или иного способа может быть не оправданным. В данной статье я рассмотрю 2 наиболее распространенных способа.

Поиск перебором значений

Довольно простой в реализации способ. Например, найти в колонке «A» ячейку, содержащую «123» можно примерно так:

Sheets("Данные").Select
For y = 1 To Cells.SpecialCells(xlLastCell).Row
    If Cells(y, 1) = "123" Then
        Exit For
    End If
Next y
MsgBox "Нашел в строке: " + CStr(y)

Минусами этого так сказать «классического» способа являются: медленная работа и громоздкость. А плюсом является его гибкость, т.к. таким способом можно реализовать сколь угодно сложные варианты поиска с различными вычислениями и т.п.

Поиск функцией Find

Гораздо быстрее обычного перебора и при этом довольно гибкий. В простейшем случае, чтобы найти в колонке A ячейку, содержащую «123» достаточно такого кода:

Sheets("Данные").Select
Set fcell = Columns("A:A").Find("123")
If Not fcell Is Nothing Then
    MsgBox "Нашел в строке: " + CStr(fcell.Row)
End If

Вкратце опишу что делают строчки данного кода:
1-я строка: Выбираем в книге лист «Данные»;
2-я строка: Осуществляем поиск значения «123» в колонке «A», результат поиска будет в fcell;
3-я строка: Если удалось найти значение, то fcell будет содержать Range-объект, в противном случае — будет пустой, т.е. Nothing.

Полностью синтаксис оператора поиска выглядит так:

Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)

What — Строка с текстом, который ищем или любой другой тип данных Excel

After — Ячейка, после которой начать поиск. Обратите внимание, что это должна быть именно единичная ячейка, а не диапазон. Поиск начинается после этой ячейки, а не с нее. Поиск в этой ячейке произойдет только когда весь диапазон будет просмотрен и поиск начнется с начала диапазона и до этой ячейки включительно.

LookIn — Тип искомых данных. Может принимать одно из значений: xlFormulas (формулы), xlValues (значения), или xlNotes (примечания).

LookAt — Одно из значений: xlWhole (полное совпадение) или xlPart (частичное совпадение).

SearchOrder — Одно из значений: xlByRows (просматривать по строкам) или xlByColumns (просматривать по столбцам)

SearchDirection — Одно из значений: xlNext (поиск вперед) или xlPrevious (поиск назад)

MatchCase — Одно из значений: True (поиск чувствительный к регистру) или False (поиск без учета регистра)

MatchByte — Применяется при использовании мультибайтных кодировок: True (найденный мультибайтный символ должен соответствовать только мультибайтному символу) или False (найденный мультибайтный символ может соответствовать однобайтному символу)

SearchFormat — Используется вместе с FindFormat. Сначала задается значение FindFormat (например, для поиска ячеек с курсивным шрифтом так: Application.FindFormat.Font.Italic = True), а потом при использовании метода Find указываем параметр SearchFormat = True. Если при поиске не нужно учитывать формат ячеек, то нужно указать SearchFormat = False.

Чтобы продолжить поиск, можно использовать FindNext (искать «далее») или FindPrevious (искать «назад»).

Примеры поиска функцией Find

Пример 1: Найти в диапазоне «A1:A50» все ячейки с текстом «asd» и поменять их все на «qwe»

With Worksheets(1).Range("A1:A50")
  Set c = .Find("asd", LookIn:=xlValues)
  Do While Not c Is Nothing
    c.Value = "qwe"
    Set c = .FindNext(c)
  Loop
End With

Обратите внимание: Когда поиск достигнет конца диапазона, функция продолжит искать с начала диапазона. Таким образом, если значение найденной ячейки не менять, то приведенный выше пример зациклится в бесконечном цикле. Поэтому, чтобы этого избежать (зацикливания), можно сделать следующим образом:

Пример 2: Правильный поиск значения с использованием FindNext, не приводящий к зацикливанию.

With Worksheets(1).Range("A1:A50")
  Set c = .Find("asd", lookin:=xlValues)
  If Not c Is Nothing Then
    firstResult = c.Address
    Do
      c.Font.Bold = True
      Set c = .FindNext(c)
      If c Is Nothing Then Exit Do
    Loop While c.Address <> firstResult
  End If
End With

В ниже следующем примере используется другой вариант продолжения поиска — с помощью той же функции Find с параметром After. Когда найдена очередная ячейка, следующий поиск будет осуществляться уже после нее. Однако, как и с FindNext, когда будет достигнут конец диапазона, Find продолжит поиск с его начала, поэтому, чтобы не произошло зацикливания, необходимо проверять совпадение с первым результатом поиска.

Пример 3: Продолжение поиска с использованием Find с параметром After.

With Worksheets(1).Range("A1:A50")
  Set c = .Find("asd", lookin:=xlValues)
  If Not c Is Nothing Then
    firstResult = c.Address
    Do
      c.Font.Bold = True
      Set c = .Find("asd", After:=c, lookin:=xlValues)
      If c Is Nothing Then Exit Do
    Loop While c.Address <> firstResult
  End If
End With

Следующий пример демонстрирует применение SearchFormat для поиска по формату ячейки. Для указания формата необходимо задать свойство FindFormat.

Пример 4: Найти все ячейки с шрифтом «курсив» и поменять их формат на обычный (не «курсив»)

lLastRow = Cells.SpecialCells(xlLastCell).Row
lLastCol = Cells.SpecialCells(xlLastCell).Column
Application.FindFormat.Font.Italic = True
With Worksheets(1).Range(Cells(1, 1), Cells(lLastRow, lLastCol))
  Set c = .Find("", SearchFormat:=True)
  Do While Not c Is Nothing
    c.Font.Italic = False
    Set c = .Find("", After:=c, SearchFormat:=True)
  Loop
End With

Примечание: В данном примере намеренно не используется FindNext для поиска следующей ячейки, т.к. он не учитывает формат (статья об этом: https://support.microsoft.com/ru-ru/kb/282151)

Коротко опишу алгоритм поиска Примера 4. Первые две строки определяют последнюю строку (lLastRow) на листе и последний столбец (lLastCol). 3-я строка задает формат поиска, в данном случае, будем искать ячейки с шрифтом Italic. 4-я строка определяет область ячеек с которой будет работать программа (с ячейки A1 и до последней строки и последнего столбца). 5-я строка осуществляет поиск с использованием SearchFormat. 6-я строка — цикл пока результат поиска не будет пустым. 7-я строка — меняем шрифт на обычный (не курсив), 8-я строка продолжаем поиск после найденной ячейки.

Хочу обратить внимание на то, что в этом примере я не стал использовать «защиту от зацикливания», как в Примерах 2 и 3, т.к. шрифт меняется и после «прохождения» по всем ячейкам, больше не останется ни одной ячейки с курсивом.

Свойство FindFormat можно задавать разными способами, например, так:

With Application.FindFormat.Font 
  .Name = "Arial" 
  .FontStyle = "Regular" 
  .Size = 10 
End With

Поиск последней заполненной ячейки с помощью Find

Следующий пример — применение функции Find для поиска последней ячейки с заполненными данными. Использованные в Примере 4 SpecialCells находит последнюю ячейку даже если она не содержит ничего, но отформатирована или в ней раньше были данные, но были удалены.

Пример 5: Найти последнюю колонку и столбец, заполненные данными

Set c = Worksheets(1).UsedRange.Find("*", SearchDirection:=xlPrevious)
If Not c Is Nothing Then
  lLastRow = c.Row: lLastCol = c.Column 
Else
  lLastRow = 1: lLastCol = 1
End If
MsgBox "lLastRow=" & lLastRow & " lLastCol=" & lLastCol

В этом примере используется UsedRange, который так же как и SpecialCells возвращает все используемые ячейки, в т.ч. и те, что были использованы ранее, а сейчас пустые. Функция Find ищет ячейку с любым значением с конца диапазона.

Поиск по шаблону (маске)

При поиске можно так же использовать шаблоны, чтобы найти текст по маске, следующий пример это демонстрирует.

Пример 6: Выделить красным шрифтом ячейки, в которых текст начинается со слова из 4-х букв, первая и последняя буквы «т», при этом после этого слова может следовать любой текст.

With Worksheets(1).Cells
  Set c = .Find("т??т*", LookIn:=xlValues, LookAt:=xlWhole)
  If Not c Is Nothing Then
    firstResult = c.Address
    Do
      c.Font.Color = RGB(255, 0, 0)
      Set c = .FindNext(c)
      If c Is Nothing Then Exit Do
    Loop While c.Address <> firstResult
  End If
End With

Для поиска функцией Find по маске (шаблону) можно применять символы:
* — для обозначения любого количества любых символов;
? — для обозначения одного любого символа;
~ — для обозначения символов *, ? и ~. (т.е. чтобы искать в тексте вопросительный знак, нужно написать ~?, чтобы искать именно звездочку (*), нужно написать ~* и наконец, чтобы найти в тексте тильду, необходимо написать ~~)

Поиск в скрытых строках и столбцах

Для поиска в скрытых ячейках нужно учитывать лишь один нюанс: поиск нужно осуществлять в формулах, а не в значениях, т.е. нужно использовать LookIn:=xlFormulas

Поиск даты с помощью Find

Если необходимо найти текущую дату или какую-то другую дату на листе Excel или в диапазоне с помощью Find, необходимо учитывать несколько нюансов:

  • Тип данных Date в VBA представляется в виде #[месяц]/[день]/[год]#, соответственно, если необходимо найти фиксированную дату, например, 01 марта 2018 года, необходимо искать #3/1/2018#, а не «01.03.2018»
  • В зависимости от формата ячеек, дата может выглядеть по-разному, поэтому, чтобы искать дату независимо от формата, поиск нужно делать не в значениях, а в формулах, т.е. использовать LookIn:=xlFormulas

Приведу несколько примеров поиска даты.

Пример 7: Найти текущую дату на листе независимо от формата отображения даты.

d = Date
Set c = Cells.Find(d, LookIn:=xlFormulas, LookAt:=xlWhole)
If Not c Is Nothing Then
  MsgBox "Нашел"
Else
  MsgBox "Не нашел"
End If

Пример 8: Найти 1 марта 2018 г.

d = #3/1/2018#
Set c = Cells.Find(d, LookIn:=xlFormulas, LookAt:=xlWhole)
If Not c Is Nothing Then
  MsgBox "Нашел"
Else
  MsgBox "Не нашел"
End If

Искать часть даты — сложнее. Например, чтобы найти все ячейки, где месяц «март», недостаточно искать «03» или «3». Не работает с датами так же и поиск по шаблону. Единственный вариант, который я нашел — это выбрать формат в котором месяц прописью для ячеек с датами и искать слово «март» в xlValues.

Тем не менее, можно найти, например, 1 марта независимо от года.

Пример 9: Найти 1 марта любого года.

d = #3/1/1900#
Set c = Cells.Find(Format(d, "m/d/"), LookIn:=xlFormulas, LookAt:=xlPart)
If Not c Is Nothing Then
  MsgBox "Нашел"
Else
  MsgBox "Не нашел"
End If

title keywords f1_keywords ms.prod api_name ms.assetid ms.date ms.localizationpriority

Range.Find method (Excel)

vbaxl10.chm144128

vbaxl10.chm144128

excel

Excel.Range.Find

d9585265-8164-cb4d-a9e3-262f6e06b6b8

08/14/2019

high

Range.Find method (Excel)

Finds specific information in a range.

[!includeAdd-ins note]

Syntax

expression.Find (What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)

expression A variable that represents a Range object.

Parameters

Name Required/Optional Data type Description
What Required Variant The data to search for. Can be a string or any Microsoft Excel data type.
After Optional Variant The cell after which you want the search to begin. This corresponds to the position of the active cell when a search is done from the user interface.

Notice that After must be a single cell in the range. Remember that the search begins after this cell; the specified cell isn’t searched until the method wraps back around to this cell.

If you don’t specify this argument, the search starts after the cell in the upper-left corner of the range.

LookIn Optional Variant Can be one of the following XlFindLookIn constants: xlFormulas, xlValues, xlComments, or xlCommentsThreaded.
LookAt Optional Variant Can be one of the following XlLookAt constants: xlWhole or xlPart.
SearchOrder Optional Variant Can be one of the following XlSearchOrder constants: xlByRows or xlByColumns.
SearchDirection Optional Variant Can be one of the following XlSearchDirection constants: xlNext or xlPrevious.
MatchCase Optional Variant True to make the search case-sensitive. The default value is False.
MatchByte Optional Variant Used only if you have selected or installed double-byte language support. True to have double-byte characters match only double-byte characters. False to have double-byte characters match their single-byte equivalents.
SearchFormat Optional Variant The search format.

Return value

A Range object that represents the first cell where that information is found.

Remarks

This method returns Nothing if no match is found. The Find method does not affect the selection or the active cell.

The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use this method. If you don’t specify values for these arguments the next time you call the method, the saved values are used. Setting these arguments changes the settings in the Find dialog box, and changing the settings in the Find dialog box changes the saved values that are used if you omit the arguments. To avoid problems, set these arguments explicitly each time you use this method.

Use the FindNext and FindPrevious methods to repeat the search.

When the search reaches the end of the specified search range, it wraps around to the beginning of the range. To stop a search when this wraparound occurs, save the address of the first found cell, and then test each successive found-cell address against this saved address.

To find cells that match more complicated patterns, use a For Each…Next statement with the Like operator. For example, the following code searches for all cells in the range A1:C5 that use a font whose name starts with the letters Cour. When Microsoft Excel finds a match, it changes the font to Times New Roman.

For Each c In [A1:C5] If c.Font.Name Like "Cour*" Then c.Font.Name = "Times New Roman" End If Next`

Examples

This example finds all cells in the range A1:A500 in worksheet one that contain the value 2, and changes the entire cell value to 5. That is, the values 1234 and 99299 both contain 2 and both cell values will become 5.

Sub FindValue()
    
    Dim c As Range
    Dim firstAddress As String

    With Worksheets(1).Range("A1:A500") 
        Set c = .Find(2, lookin:=xlValues) 
        If Not c Is Nothing Then 
            firstAddress = c.Address 
            Do 
                c.Value = 5 
                Set c = .FindNext(c) 
            Loop While Not c Is Nothing
        End If 
    End With
    
End Sub

This example finds all cells in the range A1:A500 on worksheet one that contain the substring «abc» and then replaces «abc» with «xyz».

Sub FindString()

    Dim c As Range
    Dim firstAddress As String

    With Worksheets(1).Range("A1:A500")
        Set c = .Find("abc", LookIn:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                c.Value = Replace(c.Value, "abc", "xyz")
                Set c = .FindNext(c)
            Loop While Not c Is Nothing
        End If
    End With

End Sub

[!includeSupport and feedback]

Понравилась статья? Поделить с друзьями:
  • Накладная на отпуск запасов на сторону форма 3 2 excel
  • Найти цифру в таблице excel
  • Накладная на оказание услуг образец скачать word
  • Найти цикл в excel
  • Накладная на вывоз бланк скачать word