VBA 교체 문자열 | VBA를 사용하여 문자열의 텍스트를 바꾸는 방법?

Excel VBA 교체 문자열

바꾸기는 워크 시트 기능과 VBA 기능입니다. 이 함수는 문자열의 특정 단어를 다른 문자열로 바꾸는 데 도움이됩니다. VBA의 대체 기능과 유사하게 작동합니다.

테스트 문자열 또는 텍스트 데이터 값을 처리하는 동안 두 개의 셀 데이터를 하나로 결합하거나 하나의 셀 데이터를 여러 항목으로 분할하는 등 어떤 것을 다른 것으로 바꾸거나 대체하는 것은 분명합니다. 이것들은 우리가 매일 직장에서하는 일반적인 작업입니다.

그렇다면 문자열의 한 단어를 다른 단어로 대체하는 방법은 무엇입니까? 예를 들어 문자열이 "India is a development country and India in the Asian Country"인 경우이 문자열에서 "India"라는 단어를 바꾸고 "Bharath"로 변경해야합니다.

바꾸기 기능을 사용하면 가능합니다. 이 기사에서는 VBA 코딩에서 문자열을 바꾸는 방법을 보여줍니다.

기능 교체

  • 표현식 : 이것은 우리가 무언가를 무언가로 바꾸려는 원래 문자열 값에 지나지 않습니다. 아래의 예는 "인도는 개발 도상국이고 인도는 아시아 국가입니다"라는 표현식 문자열입니다.
  • 문자열 찾기 : 대체하려는 문자열은 무엇입니까? 예를 들어 표현식 문자열 에서 "India"라는 단어를 바꾸려고합니다.
  • 문자열 바꾸기 : 찾기 문자열을 대체 할 대체 문자열은 무엇입니까 ? 따라서이 경우에는“India”라는 단어를“Bharath”로 바꾸려고합니다.
  • [시작] : 선택적 매개 변수입니다. 위의 문자열 (표현식)에는“India”라는 두 단어가 있으므로 찾기 문자열 의 어느 위치 에서 대체 프로세스를 시작해야합니까? 예를 들어, 2라고하면 두 번째 위치부터 "India"라는 단어를 대체하기 시작합니다.
  • [Count] : Expression 에서 Find String 이 여러 번 나타나는 경우 대체해야하는 단어 수.

예를 들어 "India"라는 단어가 5 번 나타나고 개수를 3으로 제공하면 처음 3 개의 "India"단어 만 대체됩니다.

VBA를 사용하여 문자열의 텍스트를 바꾸는 방법?

이 VBA 교체 문자열 Excel 템플릿은 여기에서 다운로드 할 수 있습니다. – VBA 교체 문자열 Excel 템플릿

예 1

이제 아래 문자열 값에서 "India"라는 단어를 "Bharath"로 바꾸려고합니다.

“인도는 개발 도상국이고 아시아 국가의 인도”

먼저 지금 엑셀 매크로 절차를 시작하십시오.

암호:

 Sub Replace_Example () End Sub 

VBA 변수를 문자열로 정의하십시오.

암호:

 Sub Replace_Example () Dim NewString As String End Sub 

이 변수에서 "India"라는 단어를 "Bharath"로 바꾼 후 새 문자열 값을 표시합니다. 이 변수의 경우 바꾸기 기능을 엽니 다.

이 함수의 첫 번째 인수는 "표현"입니다. 즉, 단어를 대체하려는 문자열이므로 "인도는 개발 도상국이고 아시아 국가의 인도"문자열을 복사하여 붙여 넣습니다.

다음 인수는“Find String”, 즉“India”와 같이 대체해야하는 단어입니다.

다음 인수는 "Replace String"입니다. 즉, "India"즉 "Bharath"라는 단어를 대체해야하는 문자열입니다.

좋아, 지금은 나머지 인수를 무시하십시오. 이제 메시지 상자에 결과를 표시합니다.

암호:

 Sub Replace_Example () Dim NewString As String NewString = Replace ( "인도는 개발 도상국이고 인도는 아시아 국가입니다", "인도", "Bharath") MsgBox NewString End Sub 

F5 키를 사용하거나 수동으로 코드를 실행하고 새 문자열 결과를 확인하겠습니다.

좋습니다. "India"라는 단어가있을 때마다 "Bharath"라는 단어로 대체 된 위의 결과를보십시오.

예제 # 2

이제 동일한 코드를 변수와 함께 사용하는 방법을 살펴 보겠습니다. 아래 코드를보십시오.

암호:

 Sub Replace_Example1() Dim NewString As String Dim MyString As String Dim FindString As String Dim ReplaceString As String MyString = "India is a developing country and India is the Asian Country" FindString = "India" ReplaceString = "Bharath" NewString = Replace(MyString, FindString, ReplaceString) MsgBox NewString End Sub 

In the above code, I have declared an extra three variables.

 Dim MyString As String Dim FindString As String Dim ReplaceString As String 

For these variables I have assigned values, instead of supplying the Expression String, Find String, and Replace String we will supply only variable to the Replace function.

This code also gives the same result but the only difference is we have used variables instead of direct supply of values to the function.

Example #3

Assume you want to replace the word “India” only from the second position then we need to use the Replace function parameter [“Start”]. Look at the below code for your information.

Code:

 Sub Replace_Example2() Dim NewString As String Dim MyString As String Dim FindString As String Dim ReplaceString As String MyString = "India is a developing country and India is the Asian Country" FindString = "India" ReplaceString = "Bharath" NewString = Replace(MyString, FindString, ReplaceString, Start:=34) MsgBox NewString End Sub 

Only one extra thing we have added from the previous code is the “Start” parameter as 34. Now run the code and see the result.

Now we can see only string after the 34th character of the string with “India” replacing with “Bharath”.

Example #4

Now for an example, if we want to replace only the first occurrence of the word “India” to “Bharath” then we need to use [“Count”] parameter of the Replace function.

Below is the code for you.

Code:

 Sub Replace_Example3() Dim NewString As String Dim MyString As String Dim FindString As String Dim ReplaceString As String MyString = "India is a developing country and India is the Asian Country" FindString = "India" ReplaceString = "Bharath" NewString = Replace(MyString, FindString, ReplaceString, Count:=1) MsgBox NewString End Sub 

Run the code manually or through the F5 key and see the result.

As you can see above it has replaced only the first occurrence of the word “India” to “Bharath” and the second instance remains the same.

Things to Remember Here

  • Replace is a string function family in VBA.
  • In VBA, the replace function replaces all the supplied words with replaced string if the count parameter is not specified.
  • The start parameter will delete the number of characters supplied and show the remaining result.