VBA InStrRev | Excel VBA InStrRev関数の使用方法は?

Excel VBA INSTRREV

VBA INSTRREV関数は、「In String Reverse」の略で、検索対象の文字列の末尾(右から左)から開始して、別の文字列で最初に出現する検索文字列(部分文字列)の位置を返します。検索可能な文字列。

INSTRREV関数は、検索する必要のある文字列の末尾から検索可能な文字列の検索を開始しますが、最初から位置をカウントします。そこ1つの以上であるINSTR VBA機能(の略「は、文字列」も別の文字列内の文字列を検索し、リターン位置という)が、この機能は、我々は、検索文字列を探し、そこから文字列の先頭から検索を開始します。

INSTRREVINSTRは、どちらもMSExcelの組み込みの文字列/テキストVBA関数です。Microsoft Visual BasicEditorで任意のマクロを記述しながらそれらを使用できます。

構文

上の画像でわかるように、2つの必須引数と2つのオプション引数があります。

  • StringCheck As String:これは必須の引数です。検索対象の文字列式を指定する必要があります。
  • StringMatch As String:この引数も必須です。検索する文字列式を指定する必要があります。
  • Start As Long = -1:これはオプションの引数です。数式を指定します。デフォルトでは-1を取ります。これは、検索が最後の文字位置から開始されることを意味します。80のような正の値を指定すると、左側の80文字の文字列の末尾から検索が開始されます。
  • Compare As VbCompareMethod = vbBinaryCompare As Long:この引数はオプションです。

この引数には、次の値を指定できます。

戻り値

  1. INSTRREVの関数が返す0場合は、文字列のチェックが ゼロの長さやのある文字列一致 見つからないか、またはされていない「開始」の引数>長文字列一致
  2. 文字列チェック または文字列一致 が「Null」場合、この関数は「Null」を返します。
  3. 場合は、文字列の一致がゼロの長さのものであり、その後、関数が戻るとする開始します
  4. 場合ストリング一致が列チェック内で発見されこの関数は、一致が見つかった位置を返します。

VBA INSTRREV関数の使用方法は?

このVBAINSTRREV Excelテンプレートはここからダウンロードできます– VBA INSTRREVExcelテンプレート

映画の名前とその監督のデータがあるとします。監督の名前を分けたい。

1201行のデータがあります。このタスクを手動で行うと、かなりの時間がかかります。

同じことを行うには、VBAコードを使用します。手順は次のとおりです。

  • 「開発者」タブの「コード」グループにある「VisualBasic コマンドをクリックするか、Alt + F11を押してVisualBasicエディターを開く必要があります。

  • 私たちは、挿入するモジュールを使用して「挿入」メニューを

  • 'SplittingNames'という名前サブルーチンを作成します。

  • We need 6 variables. One for storing the values of the cells, which we will manipulate. Second for storing the position of first space in the string, third for storing the position of last space in the string, fourth for storing last row number, fifth and sixth for row and column which we will use to print the values in adjacent cells.

  • To find out the last used row in the sheet, we need to use the following code.

This code will first select the cell B1 and then select the last used cell in the same column and then we assign the row number of the cell to the ‘LastRow’ variable.

  • Now to manipulate all the cells in the B column, we will run a ‘for’ loop.

  • We will store the value of cells of B column from row 2 to row 1201 one by one in ‘s’ variable to manipulate them.

  • We need to set the value of variable ‘Column’ to 3 as we need to write the split names in C (3rd Column) and a column onward.

  • If the string is only one word that means there is no space in string then we want the string itself as output. For this, we will specify the condition using ‘If and Else statement’ with an asterisk sign (denoting one or more characters) as follows:

  • If there is space in the string then we want to split the string. To do the same we have used INSTR and INSTRREV function both to find out the first space position and last space position respectively. It will help us to find the first word and last word in the string respectively.

INSTR Function takes the argument as below:

Argument Details

  • Start: From which position to start.
  • String1: We need to give string expression being searched.
  • String2: We need to specify string expression being searched for.

Compared as VbCompareMethod: Specifying Comparing method. By default, it is binary compare.

  • We need to use the vba LEFT function to extract left characters from the string. We have used ‘Last Space-1’ to get the left characters before the last space.

We need to use the RIGHT and LEN functions to extract the right characters from the string after the first space.

Macro is written. Now we just need to run the macro using the F5 key.

Code:

 Sub SplittingNames() Dim s As String Dim FirstSpace As Long Dim LastSPace As Long Dim LastRow As Long Dim Row As Long Dim Column As Long Sheet1.Range("B1").Select Selection.End(xlDown).Select LastRow = ActiveCell.Row For Row = 2 To LastRow s = Sheet1.Cells(Row, 2).Value Column = 3 If s Like "* *" Then FirstSpace = InStr(1, s, " ") LastSPace = InStrRev(s, " ") Sheet1.Cells(Row, Column).Value = Left(s, LastSPace - 1) Sheet1.Cells(Row, Column + 1).Value = Right(s, Len(s) - FirstSpace) Else Sheet1.Cells(Row, Column).Value = s End If Next End Sub 

We have a result now.