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

Excel VBAMID関数

VBA MID関数は、指定された文または単語の途中から値を抽出します。MID関数は文字列とテキスト関数に分類されます。これはワークシート関数です。つまり、VBAでこの関数を使用するには、application.worksheetメソッドを使用する必要があります。

名、姓、またはミドルネームを抽出したい場合があります。このような状況では、TEXTカテゴリの数式が要件を満たすのに役立ちます。この関数の使用法はワークシート参照の使用法と同じであり、構文も同じです。

構文

Excel MID関数と同様に、VBAでも同様の構文値のセットがあります。以下は構文です。

  • 検索する文字列:これは、文字列の文、つまり値を抽出する文字列または単語に他なりません。
  • 開始位置:文のどの位置から抽出するか。これは数値である必要があります。
  • 抽出する文字数開始位置から何文字抽出しますか?これも数値である必要があります。

VBA MID関数の使用方法は?

このVBAMID関数テンプレートはここからダウンロードできます– VBAMID関数テンプレート

例1

「HelloGoodMorning」という単語があり、この文から「Good」を抽出したいとします。以下の手順に従って、値を抽出します。

ステップ1:最初にマクロ名を作成します。

コード:

 Sub MID_VBA_Example1()End Sub 

ステップ2:変数を「STRING」として宣言します。

コード:

 Sub MID_VBA_Example1()Dim MiddleValue As String End Sub 

ステップ3: MID関数を使用してこの変数に値を割り当てます。

コード:

 Sub MID_VBA_Example1()Dim MiddleValue As String MiddleValue = Mid(End Sub 

ステップ4:最初の引数は文字列です。つまり、どの値から抽出するかです。だから私たちの価値は「こんにちはおはよう」です。

コード:

 Sub MID_VBA_Example1()Dim MiddleValue As String MiddleValue = Mid( "Hello Good Morning"、End Sub 

ステップ5:次は、抽出する文字の開始位置です。この場合、おはようは7番目の文字から始まります。

注:スペースも文字です。

コード:

 Sub MID_VBA_Example1()Dim MiddleValue As String MiddleValue = Mid( "Hello Good Morning"、7 End Sub 

ステップ6:長さは、抽出する文字数に他なりません。「Good」という単語の長さは4文字なので、ここでは4文字を抽出する必要があります。

コード:

 Sub MID_VBA_Example1()Dim MiddleValue As String MiddleValue = Mid( "Hello Good Morning"、7、4)End Sub 

ステップ7:数式が完成しました。メッセージボックスに変数の結果を表示しましょう。

コード:

 Sub MID_VBA_Example1()Dim MiddleValue As String MiddleValue = Mid( "Hello Good Morning"、7、4)MsgBox MiddleValue End Sub 

ステップ8:このコードを手動で実行するか、F5キーを押すと、メッセージボックスに「Good」という単語が表示されます。

出力:

例2

Assume you have a first name and last name together and the word is “Ramesh, Tendulkar”. Between First Name & Last Name, separation character is a comma (,). Now we need to extract the first name only.

Step 1: Create a macro and define a variable.

Code:

 Sub MID_VBA_Example2()     Dim FirstName As String End Sub 

Step 2: Now assign a value to this variable through MID function.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid( End Sub 

Step 3: Our string is “Ramesh.Tendulkar”, so enter this word.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar", End Sub 

Step 4: Since we are extracting the first name starting position is 1.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar",1, End Sub 

Step 5: Length of the character you can directly enter as 6 but this is not the best way. In order to determine the length lets apply one more formula called Instr.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar",1,InStr( End Sub 

Step 6: For this starting position is 1.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar",1,InStr(1, End Sub 

Step 7: String 1 is our name i.e. “Ramesh, Tendulkar”.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar",1,InStr(1,"Ramesh,Tendulkar", End Sub 

Step 8: String 2 what is the separator of first name & last name i.e. comma (,).

Code:

 Sub MID_VBA_Example2()     Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar",1,InStr(1,"Ramesh,Tendulkar",",") End Sub 

Note: Instr function will return how many characters are there in the word “Ramesh, Tendulkar” from the string 1 position to the string 2 positions i.e. until comma (,). So Instr will return 7 as the result including comma (,).

Step 9: Since Instr function returns no., of characters including comma (,) we need to minus 1 character here. So enter -1 after the close of Instr function.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar", 1, InStr(1, "Ramesh,Tendulkar", ",") - 1) End Sub 

Step 10: Now show the value of the variable in the message box.

Code:

 Sub MID_VBA_Example2() Dim FirstName As String FirstName = Mid("Ramesh,Tendulkar", 1, InStr(1, "Ramesh,Tendulkar", ",") - 1) MsgBox FirstName End Sub 

Step 11: Run this code using F5 key or you can run this code manually, we would get the first name in the message box.

Output:

Example #3

Now I will give you one assignment to solve. I have a list of First Name & Last Name.

From this list I want you to extract the first name only. All the best!!!!.

Ok, If you have tried and not able to get the result then below code would help you in this.

Code:

 Sub MID_VBA_Example3()     Dim   i   As Long For i = 2   To  15 Cells(i, 2).Value = Mid(Cells(i, 1).Value, 1, InStr(1, Cells(i, 1).Value, ",") - 1)     Next i End Sub 

Copy & Paste the above code in your module. After copying the code, run this code using the F5 key or you can run manually.

It should give the result like the below.

Things to Remember

  • Length argument in MID function is optional. If you ignore this it will take 1 as the default value.
  • In order to determine the length or starting position use Instr function along with MID function.