VBA MID 기능 | Excel VBA MID 기능을 사용하는 방법?

Excel VBA MID 기능

VBA MID 함수는 제공된 문장 또는 단어의 중간에서 값을 추출합니다. MID 함수는 String 및 Text 함수로 분류되며 VBA에서이 함수를 사용하려면 application.worksheet 메소드를 사용해야하는 워크 시트 함수입니다.

이름, 성 또는 중간 이름을 추출하려는 상황이 있습니다. 이러한 상황에서 TEXT 범주 수식은 요구 사항을 충족하는 데 도움이됩니다. 이 함수의 사용은 워크 시트 참조의 사용과 동일하며 구문도 동일합니다.

통사론

엑셀 MID 함수와 마찬가지로 VBA에서도 유사한 구문 값 세트가 있습니다. 아래는 구문입니다.

  • 검색 할 문자열 : 이것은 문자열의 문장입니다. 즉, 값을 추출하려는 문자열이나 단어입니다.
  • 시작 위치 : 추출하려는 문장의 위치. 숫자 값이어야합니다.
  • 추출 할 문자 수 : 시작 위치에서 추출 할 문자 수는? 이것은 또한 숫자 값이어야합니다.

VBA MID 기능은 어떻게 사용하나요?

이 VBA MID 기능 템플릿은 여기에서 다운로드 할 수 있습니다 – VBA MID 기능 템플릿

예 1

"Hello Good Morning"이라는 단어가 있고이 문장에서 "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 단계 : 첫 번째 인수는 문자열입니다. 즉, 추출하려는 값입니다. 그래서 우리의 가치는“Hello Good Morning”입니다.

암호:

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

5 단계 : 다음은 추출하려는 캐릭터의 시작 위치입니다. 이 경우 Good morning은 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.