Pravá funkcia VBA (príklady) Sprievodca krok za krokom k Excel VBA vpravo

Správna funkcia vo VBA Excel

Pravá funkcia je rovnaká ako vo funkcii listu aj vo VBA, táto funkcia sa používa tak, že nám poskytuje podreťazec z daného reťazca, ale vyhľadávanie sa vykonáva sprava doľava od reťazca, toto je typ reťazcovej funkcie vo VBA. používa sa s metódou funkcie application.worksheet.

PRAVÁ Funkcia v programe Excel VBA, ktorá sa používa na extrakciu znakov z pravej strany zadaných textových hodnôt. V programe Excel máme veľa textových funkcií na prácu s textovými údajmi. Niektoré z užitočných funkcií sú funkcia LEN, LEFT, RIGHT, MID na extrahovanie znakov z textových hodnôt. Bežným príkladom použitia týchto funkcií je extrahovanie krstného mena a priezviska oddelene od celého mena.

PRAVÝ vzorec je tiež v pracovnom hárku. Vo VBA sa musíme pri prístupe k funkcii VBA RIGHT spoľahnúť na triedu funkcií pracovného listu, skôr máme zabudovanú funkciu RIGHT aj vo VBA.

Teraz sa pozrite na nižšie uvedenú syntax vzorca VBA RIGHT.

  • Reťazec: Toto je naša hodnota a z tejto hodnoty sa pokúšame extrahovať znaky z pravej strany reťazca.
  • Dĺžka: Z dodávaného reťazca koľko znakov potrebujeme. Ak potrebujeme štyri znaky z pravej strany, môžeme argument dodať ako 4.

Napríklad ak je reťazec „Mobilný telefón“ a ak chceme extrahovať iba slovo „Telefón“, môžeme uviesť argument, ako je uvedené nižšie.

VPRAVO („Mobilný telefón“, 5)

Dôvod, prečo som spomenul 5, pretože slovo „Telefón“ má 5 písmen. V ďalšej časti článku si ukážeme, ako to môžeme použiť vo VBA.

Príklady funkcie Excel VBA Right

Nasledujú príklady správnych funkcií VBA Excel.

Túto šablónu Excel s pravými funkciami VBA si môžete stiahnuť tu - šablónu Excel s pravými funkciami VBA

Príklad č

Ukážem vám jednoduchý príklad na začatie konania. Predpokladajme, že máte reťazec „New York“ a ak chcete z pravej strany extrahovať 3 znaky, podľa nasledujúcich pokynov napíšte kód.

Krok 1: Deklarujte premennú ako reťazec VBA.

Kód:

 Sub Right_Example1 () Dim k As String End Sub 

Krok 2: Teraz pre túto premennú priradíme hodnotu použitím funkcie RIGHT.

Kód:

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

Krok 3: Prvý  argument je reťazec a náš reťazec pre tento príklad je „New York“.

Kód:

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

Krok 4: Ďalej nasleduje počet znakov, ktoré potrebujeme z dodaného reťazca. V tomto príklade potrebujeme 3 znaky.

Kód:

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

Krok 5: Máme na výber 2 argumenty, takže sme hotoví. Teraz priraďte hodnotu tejto premennej do okna správy vo VBA.

Kód:

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

Spustite kód pomocou klávesu F5 alebo ručne a pozrite si výsledok v okne správy.

V slove „New York“ z pravej strany sú 3 znaky „ork“.

Teraz zmením dĺžku z 3 na 4, aby som získal celú hodnotu.

Kód:

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

Spustite tento kód manuálne alebo pomocou klávesu F5 a dostaneme „York“.

Príklad č

Teraz sa pozrite na ešte jeden príklad, tentokrát považujte hodnotu reťazca za „Michael Clarke“.

Ak zadáte dĺžku ako 6, dostaneme ako výsledok „Clarke“.

Kód:

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

Spustite tento kód pomocou klávesu F5 alebo ručne, aby ste videli výsledok.

Funkcia Dynamic Right v Exceli VBA

Ak sledujete naše predchádzajúce dva príklady, čísla argumentov dĺžky sme zadali ručne. Toto však nie je správny postup na vykonanie tejto práce.

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.