VBA Right Function(例)| Excel VBARightのステップバイステップガイド

VBAExcelの正しい機能

右関数はワークシート関数とVBAの両方と同じです。この関数の使用法は、指定された文字列から部分文字列を取得することですが、検索は文字列の右から左に実行されます。これはVBAの文字列関数の一種です。 application.worksheet関数メソッドで使用されます。

指定されたテキスト値の右側から文字を抽出するために使用されるExcelVBAのRIGHT関数。Excelには、テキストベースのデータを処理するための多くのテキスト関数があります。便利な関数には、テキスト値から文字を抽出するためのLEN、LEFT、RIGHT、MID関数があります。これらの関数を使用する一般的な例は、フルネームとは別に姓名を抽出することです。

右の式もワークシートにあります。VBAでは、ワークシート関数クラスに依存してVBA RIGHT関数にアクセスする必要がありますが、VBAにも組み込みのRIGHT関数があります。

次に、以下のVBARIGHT式の構文を見てください。

  • 文字列:これは私たちの値であり、この値から、文字列の右側から文字を抽出しようとしています。
  • 長さ:指定された文字列から必要な文字数。右側から4文字が必要な場合は、引数を4として指定できます。

たとえば、文字列が「携帯電話」で、「電話」という単語のみを抽出する場合は、次のような引数を指定できます。

右(「携帯電話」、5)

「電話」という言葉に5文字入っているので5と言った理由。記事の次のセクションでは、VBAでどのように使用できるかを説明します。

ExcelVBAの正しい関数の例

以下は、Right Function VBAExcelの例です。

このVBARight Function Excelテンプレートはここからダウンロードできます– VBA Right FunctionExcelテンプレート

例1

手続きを開始するための簡単な例を示します。文字列「NewYork」があり、右から3文字を抽出する場合は、以下の手順に従ってコードを記述します。

手順1:変数をVBA文字列として宣言します。

コード:

 Sub Right_Example1()Dim k As String End Sub 

ステップ2:この変数には、RIGHT関数を適用して値を割り当てます。

コード:

 Sub Right_Example1()Dim k As String k = Right(End Sub 

ステップ3:最初の 引数はStringであり、この例の文字列は「NewYork」です。

コード:

 Sub Right_Example1()Dim k As String k = Right( "New York"、End Sub 

ステップ4:次は、提供された文字列から必要な文字数です。この例では、3文字が必要です。

コード:

 Sub Right_Example1()Dim k As String k = Right( "New York"、3)End Sub 

ステップ5:対処すべき2つの引数があるので、これで完了です。次に、VBAのメッセージボックスでこの変数の値を割り当てます。

コード:

 Sub Right_Example1()Dim k As String k = Right( "New York"、3)MsgBox k End Sub 

F5キーを使用して、または手動でコードを実行し、メッセージボックスに結果を表示します。

右側から「ニューヨーク」という言葉では、3文字が「オーク」です。

Now I will change the length from 3 to 4 to get the full value.

Code:

 Sub Right_Example1() Dim k As String k = Right("New York", 4) MsgBox k End Sub 

Run this code manually or using F5 key then, we will get “York”.

Example #2

Now take a look at one more example, this time consider the string value as “Michael Clarke”.

If you supply the length as 6 we will get “Clarke” as the result.

Code:

 Sub Right_Example1() Dim k As String k = Right("Michael Clarke", 6) MsgBox k End Sub 

Run this code using the F5 key or manually to see the result.

Dynamic Right Function in Excel VBA

If you observe our previous two examples we have supplied the length argument numbers manually. But this is not the right process to do the job.

In each of the string right-side characters are different in each case. We cannot refer to the length of the characters manually for each value differently. This where the other string function “Instr” plays a vital role.

Instr function returns the supplied character position in the supplied string value. For example, Instr (1,” Bangalore”,”a”) return the position of the letter “a” in the string “Bangalore” from the first (1) character.

In this case, the result is 2 because from the first character position of the letter “a” is the 2nd position.

If I change the starting position from 1 to 3 it will return 5.

Instr (3,” Bangalore”,”a”).

The reason why it returns 5 because I mentioned the starting position to look for the letter “a” only from the 3rd letter. So the position of the second appeared “a” is 5.

So, to find the space character of the of each string we can use this. Once we find the space character position we need to minus that from a total length of the string by using LEN.

For example in the string “New York” the total number of characters is 8 including space, and the position of the space character is 4th. So 8-4 = 4 right will extract 4 characters from the right.

Now, look at the below code for your reference.

Code:

 Sub Right_Example3() Dim k As String Dim L As String Dim S As String L = Len("Michael Clarke") S = InStr(1, "Michael Clarke", " ") k = Right("Michael Clarke", L - S) MsgBox k End Sub 

In the above code variable “L” will return 14 and variable “S” will return 8.

In the VBA right formula, I have applied L – S i.e. 14-8 = 6. So from right side 6 characters i.e. “Clarke”.

Loops with Right Function in Excel VBA

When we need to apply the VBA RIGHT function with many cells we need to include this inside the loops. For example, look at the below image.

We cannot apply many lines of the code to extract a string from the right side. So we need to include loops. The below code will do it for the above data.

Code:

 Sub Right_Example4() Dim k As String Dim L As String Dim S As String Dim a As Integer For a = 2 To 5 L = Len(Cells(a, 1).Value) S = InStr(1, Cells(a, 1).Value, " ") Cells(a, 2).Value = Right(Cells(a, 1), L - S) Next a End Sub 

The result of this code is as follows.