メインコンテンツへスキップ

Excel で n 番目に出現する文字を見つける – 3 つの簡単な方法

著者:シャオヤン 最終更新日:2024年04月24日

Excel でテキスト文字列内で n 番目に出現する特定の文字を見つけることは、文字列を解析したり、特定の区切り文字やパターンに基づいて情報を抽出したりする必要があるデータ分析で特に役立ちます。たとえば、テキスト文字列内で 2 番目または 3 番目に出現する文字「-」を見つけてみましょう。このタスクを効率的に実行するための簡単なテクニックを紹介します。


数式を使用してテキスト文字列内で n 番目に出現する文字を検索します

文字の n 番目の出現を検索する式を作成できます。次のようにしてください:

1. 結果を取得するセルに次の数式を入力またはコピーします。

=FIND(CHAR(1),SUBSTITUTE(A2,"-",CHAR(1),2))
Note:上記の式では:
  • A2: 文字列を含むセル。
  • -:探しているキャラクターです。
  • 2: 検索したい 2 番目の出現箇所を、必要に応じて 3,4、XNUMX に変更できます。

2. 次に、数式の入力を下にドラッグして他のセルに数式を入力すると、文字の 2 番目の位置が一度に表示されます。スクリーンショットを参照してください。

式の説明:
  • 代替 n 番目に出現する文字を印刷不可能な文字 (CHAR(1)) に置き換えます。
  • FIND この印刷不可能な文字を検索し、n 番目に出現する位置を指定します。

Kutools for Excelを使用してテキスト文字列内の文字のn番目の出現を見つけます

数式や VBA の使用が好きではない場合は、便利な代替手段を検討することをお勧めします。 Kutools for Excel。その Formula グループ内に、便利なユーティリティがあります。 文字列のN番目に文字が表示される場所を見つける これは、セル内の任意の文字の n 番目の位置をすばやく識別して返します。

Kutools for Excelをインストールした後、クリック クツール > フォーミュラヘルパー > フォーミュラヘルパー を開く フォーミュラヘルパー ダイアログボックス。結果を入れたいセルをクリックします。次に、次のようにします。

  1. 選択 見上げる のドロップダウンリストから 数式タイプ セクション;
  2. 選択する 文字列のN番目に文字が表示される場所を見つける in 式を選択してください セクション;
  3. 使用する文字列を含むセルを選択し、指定した文字と n 番目の文字列をテキスト ボックスに入力します。 引数入力 セクション;
  4. 最後に、 OK 結果を取得するためのボタン。

ユーザー定義関数を使用して、テキスト文字列内で n 番目に出現する文字を検索します。

このセクションでは、Excel で n 番目に出現する文字を検索するための UDF を作成および使用する方法を説明し、データ処理の最適化に役立つステップバイステップのガイドを提供します。

  1. 押したまま Alt + F11 キー、そしてそれは開きます アプリケーション向け Microsoft Visual Basic 窓。
  2. クリック インセット > モジュール、モジュールウィンドウに次のマクロを貼り付けます。
     Function FindN(sFindWhat As String, _
    sInputString As String, N As Integer) As Integer
    'Updateby Extendoffice
    Dim J As Integer
    Application.Volatile
    FindN = 0
    For J = 1 To N
    FindN = InStr(FindN + 1, sInputString, sFindWhat)
    If FindN = 0 Then Exit For
    Next
    End Function
  3. 次に、VBA ウィンドウを閉じます。ワークシートに戻り、次の数式をセルに入力し、フィル ハンドルを下にドラッグして他のセルに数式を入力します。スクリーンショットを参照してください。
    =FindN("-",A2,3)

  4. 関連記事:

    • 列内の単語の出現数を数える
    • 以下のスクリーンショットに示すように、いくつかの重複する値を含む列データがある場合、次に実行したいのは、この列内の特定の単語の出現回数をカウントすることです。 このチュートリアルでは、Excelですばやく解決するための秘訣をいくつか紹介します。
    • 文字列から n 番目の文字を抽出します
    • 一般に、特定の文字以降の文字列を抽出したい場合がありますが、ここでは、以下のスクリーンショットに示すように、文字列から n 番目の文字を抽出したいと考えています。
    • 文字列から最初/最後の n 文字を抽出する
    • たとえば、各セルに長い文字列を含むリストがあり、各文字列の最初の3文字のように、各文字列から最初のn文字のみを抽出したい場合、次の方法を使用してExcelで解決できます。 。

最高のオフィス生産性向上ツール

🤖 Kutools AI アシスタント: 以下に基づいてデータ分析に革命をもたらします。 インテリジェントな実行   |  コードを生成  |  カスタム数式の作成  |  データを分析してグラフを生成する  |  Kutools関数を呼び出す...
人気の機能: 重複を検索、強調表示、または識別する   |  空白行を削除する   |  データを失わずに列またはセルを結合する   |   数式なしのラウンド ...
スーパールックアップ: 複数の基準の VLookup    複数の値の VLookup  |   複数のシートにわたる VLookup   |   ファジールックアップ ....
詳細ドロップダウン リスト: ドロップダウンリストを素早く作成する   |  依存関係のドロップダウン リスト   |  複数選択のドロップダウンリスト ....
列マネージャー: 特定の数の列を追加する  |  列の移動  |  Toggle 非表示列の表示ステータス  |  範囲と列の比較 ...
注目の機能: グリッドフォーカス   |  デザインビュー   |   ビッグフォーミュラバー    ワークブックとシートマネージャー   |  リソースライブラリ (自動テキスト)   |  日付ピッカー   |  ワークシートを組み合わせる   |  セルの暗号化/復号化    リストごとにメールを送信する   |  スーパーフィルター   |   特殊フィルター (太字/斜体/取り消し線をフィルター...) ...
上位 15 のツールセット12 テキスト ツール (テキストを追加, 文字を削除する、...)   |   50+ チャート 種類 (ガントチャート、...)   |   40+ 実用的 (誕生日に基づいて年齢を計算する、...)   |   19 挿入 ツール (QRコードを挿入, パスから画像を挿入、...)   |   12 変換 ツール (数字から言葉へ, 通貨の換算、...)   |   7 マージ&スプリット ツール (高度な結合行, 分割セル、...)   |   ... もっと

Kutools for Excel で Excel スキルを強化し、これまでにない効率を体験してください。 Kutools for Excelは、生産性を向上させ、時間を節約するための300以上の高度な機能を提供します。  最も必要な機能を入手するにはここをクリックしてください...

説明


Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、Visioおよびプロジェクト。
  • 新しいウィンドウではなく、同じウィンドウの新しいタブで複数のドキュメントを開いて作成します。
  • 生産性を 50% 向上させ、毎日何百回もマウス クリックを減らすことができます!
Comments (28)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Your formula to find the Nth occurrence of a character in a string does not work. Using your example, adding 2 to the results of the imbedded find ensures a starting position for the initial find of a position beyond the 1st two c's but changing the +2 to +3 does not yield the position of the 4th "c", it simply starts the search one character deeper into the string and the result is still the 3rd "c". To find the 4th "c" the search must begin beyond the position of the 3rd "c". I look forward to your new solution.
This comment was minimized by the moderator on the site
Hello, David

Yes, as you said, the first formula does not work correctly, you can apply the second formula:
=FIND(CHAR(1),SUBSTITUTE(A2,"c",CHAR(1),2))

Note: The number "2" in the formula means the second "c", if you want to get the third, fourth "c", just change the number 2 to 3,4 as you need.
Please try, hope it can help you!
This comment was minimized by the moderator on the site
Find formula1 is imperfect. It will be a great help if it is removed. I have lost lot of time trying for 4th occurance of "a" in "vedanarayanan" Thanks.
This comment was minimized by the moderator on the site
To handle the exception of the formula, you can use same with if and iserro
=IF(ISERR(FIND("c",A1,FIND("c",A1)+2)),FIND("c",A1,FIND("c",A1)+0),FIND("c",A1,FIND("c",A1)+2))
This comment was minimized by the moderator on the site
"Substitute" lets you find the Nth occurrence of a term, so combine it with "find" as follows (where 5=5th occurrence):

=FIND(CHAR(1),SUBSTITUTE(A1,"c",CHAR(1),5))
This comment was minimized by the moderator on the site
Excellent! I am glad I scrolled up to review other comments after posting my own.
This comment was minimized by the moderator on the site
This is the correct answer! please replace this whole article with this one line of code.....
This comment was minimized by the moderator on the site
The first formula is completely useless and wrong but it works just correct for this example as the first and second "c"s are adjacent. The correct formula is:


For 2nd occurence -
=FIND("c",A1,FIND("c",A1)+1)

For 3rd occurence -
=FIND("c",A1,FIND("c",A1,FIND("c",A1)+1)+1) and so on...
This comment was minimized by the moderator on the site
Absolutely agree. The +2, or +3 etc.....entirely depends on the spacing between the n and (n+1) occurences of "c". Thus you would have to manually adjust for every cell if you were trying to copy this formula down a column for instance. Essentially the same as manually counting.


Ex. Let's try to find the 4th occurrence of "c" in cell A1 using the provided formula. Per the article we should use +3 in our nested FIND function to find the 4th occurrence.


Text in Cell A1:

"cab car abc cole cube"

Manually counting, the 4th occurrence is the "c" in cole in the character position of 13.


=FIND("c",A1,FIND("c",A1)+3)

The nested function, FIND("c",A1) will find the first occurrence of "c" in position 1.

Therefore,

FIND("c",A1)+3 = 1+3 = 4

Plugging that back into our formula,

=FIND("c",A1,FIND("c",A1)+3) = FIND("c",A1,1+3) = FIND("c",A1,4)

This new formula will look for the first occurrence of "c" beginning from character position 4. The 4th character in the string is the space just after cab.

Therefore, the formula will find the "c" in car and return the value of 5 for our formula. As we know from manually counting we are looking for 13 as the output.
This comment was minimized by the moderator on the site
The firs formula works correct in my version, in the note, it has explained how to find the third or forth or nth "c".

Note: You can change the 2 in the formula based on your needs. For example, if you want to find the fourth position of "c", you can change the 2 to 3. And if you want to find the first position of "c", you shuold change 2 to 0.
This comment was minimized by the moderator on the site
Try to find out the position of the second c with the first sentence of my above comment as the string using the first formula. You'll get to know how correct it is!
This comment was minimized by the moderator on the site
Hello all,

I have a text in an Excel cell " BY TRANSFER-NEFT*HDFC0000001*N08745987123546*J0032331*KUMAR--"

I need a function to extract just "N08745987123546"

Thanks
This comment was minimized by the moderator on the site
Hello, do you want to extract the numbers after No and the number of numbers are fixed? If so, you can use Kutools for Exccel's Extract text function, type No????to extract.
This comment was minimized by the moderator on the site
HI ALL,
I have a text in an Excel cell " BY TRANSFER-NEFT*HDHC0065431*N053112345624801*K0038331*krishna--"
I need a function to extract just "N053112345624801"
This comment was minimized by the moderator on the site
Formula 1 doesn't work on fourth, fifth, sixth etc. so (+3,4,5) will always show third position of letter "c" which is 19 second formula is working though.
This comment was minimized by the moderator on the site
Hello all, I have a text in an Excel cell "23 floyd lane, longville,KN 14564" I need a function to extract just "longville" Thanks
This comment was minimized by the moderator on the site
If you must use a function, try: =TRIM(MID(A1,SEARCH(",",A1)+1,SEARCH("!@#",SUBSTITUTE(A1,",","!@#",2))-SEARCH(",",A1)-1)) Otherwise, you might consider Data > Text to Columns > Delimited (using a comma as the separator).
This comment was minimized by the moderator on the site
Try this: =INDEX(SMALL(IF(MID(A1,MATCH(ROW(INDIRECT(1&":"&LEN(A1))),ROW(INDIRECT(1&":"&LEN(A1))),0),1)="c",MATCH(ROW(INDIRECT(1&":"&LEN(A1))),ROW(INDIRECT(1&":"&LEN(A1))),0),""),MATCH(ROW(INDIRECT(1&":"&LEN(A1)-LEN(SUBSTITUTE(A1,"c","")))),ROW(INDIRECT(1&":"&LEN(A1)-LEN(SUBSTITUTE(A1,"c","")))),0)),2) where "c" is the character required and 2 is the position. Note this is an array formula.
This comment was minimized by the moderator on the site
this is simpler: =INDEX(SMALL(IF(MID(A1,ROW(INDIRECT(1&":"&LEN(A1))),1)="c",ROW(INDIRECT(1&":"&LEN(A1))),""),ROW(INDIRECT(1&":"&LEN(A1)-LEN(SUBSTITUTE(A1,"c",""))))),2)
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations