VBA 오른쪽 기능 (예제) | Excel VBA 오른쪽에 대한 단계별 가이드

VBA Excel의 올바른 기능

오른쪽 함수는 워크 시트 함수와 VBA 모두에서와 동일합니다.이 함수의 사용은 주어진 문자열에서 하위 문자열을 제공하지만 문자열의 오른쪽에서 왼쪽으로 검색이 수행된다는 것입니다. 이것은 VBA의 문자열 함수 유형입니다. application.worksheet 함수 메서드와 함께 사용됩니다.

제공된 텍스트 값의 오른쪽에서 문자를 추출하는 데 사용되는 Excel VBA의 RIGHT 함수입니다. Excel에는 텍스트 기반 데이터를 처리하는 많은 텍스트 함수가 있습니다. 유용한 기능 중 일부는 텍스트 값에서 문자를 추출하는 LEN, LEFT, RIGHT, MID 기능입니다. 이러한 기능을 사용하는 일반적인 예는 전체 이름과 별도로 이름과 성을 추출하는 것입니다.

RIGHT 수식도 워크 시트에 있습니다. VBA에서는 워크 시트 함수 클래스에 의존하여 VBA RIGHT 함수에 액세스해야합니다. VBA에도 기본 제공 RIGHT 함수가 있습니다.

이제 VBA RIGHT 수식의 아래 구문을 살펴보십시오.

  • 문자열 : 이것이 우리의 값이고이 값에서 문자열의 오른쪽에서 문자를 추출하려고합니다.
  • 길이 : 제공된 문자열 에서 필요한 문자 수. 오른쪽에서 4 개의 문자가 필요하면 인수를 4로 제공 할 수 있습니다.

예를 들어 문자열이 "Mobile Phone"이고 "Phone"이라는 단어 만 추출하려는 경우 아래와 같은 인수를 제공 할 수 있습니다.

오른쪽 (“휴대폰”, 5)

내가 5를 언급 한 이유는“전화”라는 단어에 5 개의 글자가 들어 있기 때문입니다. 이 기사의 다음 섹션에서는 VBA에서 어떻게 사용할 수 있는지 살펴 보겠습니다.

Excel VBA 오른쪽 기능의 예

다음은 Right Function VBA Excel의 예입니다.

이 VBA 오른쪽 기능 Excel 템플릿을 여기에서 다운로드 할 수 있습니다 – VBA 오른쪽 기능 Excel 템플릿

예 1

절차를 시작하는 간단한 예를 보여 드리겠습니다. 문자열 "New York"이 있고 오른쪽에서 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이고이 예제의 문자열은 "New York"입니다.

암호:

 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 단계 : 우리는 다루어야 할 두 가지 주장이 있으므로 끝났습니다. 이제 VBA의 메시지 상자에이 변수의 값을 할당합니다.

암호:

 Sub Right_Example1 () Dim k As String k = Right ( "뉴욕", 3) MsgBox k End Sub 

F5 키를 사용하거나 수동으로 코드를 실행하고 메시지 상자에서 결과를 확인합니다.

오른쪽의 "뉴욕"이라는 단어에는 3 개의 문자가 "오크"입니다.

이제 전체 값을 얻기 위해 길이를 3에서 4로 변경합니다.

암호:

 Sub Right_Example1 () Dim k As String k = Right ( "뉴욕", 4) MsgBox k End Sub 

이 코드를 수동으로 실행하거나 F5 키를 사용하면 "York"가 표시됩니다.

예제 # 2

이제 한 가지 예를 더 살펴 보겠습니다. 이번에는 문자열 값을 "Michael Clarke"로 간주합니다.

길이를 6으로 제공하면 결과로 "Clarke"가 표시됩니다.

암호:

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

F5 키를 사용하거나 수동으로이 코드를 실행하여 결과를 확인합니다.

Excel VBA의 동적 오른쪽 함수

앞의 두 가지 예를 살펴보면 길이 인수 번호를 수동으로 제공했습니다. 그러나 이것은 작업을 수행하는 올바른 프로세스가 아닙니다.

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.