Range.Find メソッド (Excel) | Microsoft Learn

Office Add-insモデルでは、VSTOアドインよりも軽量なアドインを、HTML5やJavaScript、CSS3、XMLなどのウェブ技術を使用して開発できます。Findメソッドを使うと、特定の範囲内で値や文字列を検索できます。このメソッドはRangeオブジェクトを返し、検索条件(What、LookInなど)の設定は保存されます。Wraparoundで検索範囲の終わりに達すると最初のセルに戻ります。FindNextやFindPreviousメソッドを使って検索を繰り返し、条件に合うセルを見つけて変更する例が示されています。

Excel VBA: 特定の情報を範囲内で見つける方法

ExcelのVBAを使用して特定の情報を範囲内で検索することは、データ操作の一環として非常に役立ちます。この機能を使うことで、数多くのセルの中から必要なデータを効率的に見つけ出すことが可能です。

検索機能の概要

Findメソッドを使用することで、Excelの範囲内で指定したデータを検索することができます。以下の構文をご覧ください。

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

ここで、expressionRangeオブジェクトを表します。

パラメータの説明

Findメソッドにはいくつかのパラメータがあります。主なものは以下のとおりです。

名前 必須/オプション データ型 説明
What 必須 Variant 検索するデータ
After オプション Variant 検索を開始するセル
LookIn オプション Variant 検索対象とするセルの属性(数式、値、コメントなど)
LookAt オプション Variant セル全体または部分文字列の検索を指定
SearchOrder オプション Variant 行または列単位での検索順
SearchDirection オプション Variant 次または前方の検索を指定
MatchCase オプション Variant 大文字、小文字を区別するかどうか
MatchByte オプション Variant バイト数を考慮するかどうか(ダブルバイト言語用)
SearchFormat オプション Variant 検索フォーマット

戻り値

Findメソッドは、情報が見つかった最初のセルを表すRangeオブジェクトを返します。情報が見つからなかった場合、Nothingを返します。

使用例

次のコードは、範囲A1:A500内で値「2」を含むすべてのセルを見つけ、そのセルの値を「5」に変更します。

vba
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

また、次のコードは、範囲A1:A500内で部分文字列「abc」を探し、「xyz」に置き換えます。

vba
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

まとめ

Excel VBAのFindメソッドは、大量のデータを扱う際に非常に効果的なツールです。正しいパラメータ設定を行うことで、効率的にデータを検索し、必要な情報を見つけ出すことができます。この機能を活用して、日々の業務をよりスムーズに進めていきましょう。

————-

Range.Find method (Excel) | Microsoft Learn

Source link

Summary of the “Finds Specific Information in a Range” Method

The Find method in Excel VBA allows users to search for specific data within a defined range. This feature is particularly useful for efficiently locating values in larger datasets.

Key Points:

  • Office Add-ins Model: For cross-platform solutions, consider using Office Add-ins, which utilize web technologies like HTML and JavaScript.

Syntax:

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

  • expression: A Range object representing the target range.

Parameters:

  • What (Required): The data to be searched, which can be a string or any Excel data type.
  • After (Optional): Specifies the cell after which the search begins.
  • LookIn (Optional): Defines where to search (e.g., formulas, values).
  • LookAt (Optional): Indicates if to match the whole cell content or part of it.
  • SearchOrder (Optional): Determines the search direction (by rows or columns).
  • SearchDirection (Optional): Specifies either next or previous search direction.
  • MatchCase (Optional): If True, the search is case-sensitive.
  • MatchByte (Optional): Relevant for double-byte language support.
  • SearchFormat (Optional): Specifies format criteria for the search.

Return Value:

The method returns a Range object for the first found cell, or Nothing if the item isn’t found.

Search Behavior:

  • Adjusted settings for various parameters are retained for subsequent calls, which can be changed in the Find dialog box.
  • Use FindNext and FindPrevious to repeat searches. Be cautious of wrapping around when reaching the end of the range to avoid infinite loops.

Example Usage:

  1. Replace Values: An example finds cells in range A1:A500 containing the value 2 and changes them to 5.
    vb
    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

  2. Replace Substrings: Another example finds and replaces the substring “abc” with “xyz” in range A1:A500.
    vb
    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

For additional support or feedback about Office VBA, users are encouraged to refer to the Office VBA support resources.

関連記事