Pravá funkce VBA (příklady) - Průvodce krok za krokem k aplikaci Excel VBA vpravo

Správná funkce ve VBA Excel

Pravá funkce je stejná jako u funkce listu i VBA, použití této funkce spočívá v tom, že nám dává podřetězec z daného řetězce, ale vyhledávání se provádí zprava doleva od řetězce, jedná se o typ řetězcové funkce ve VBA používá se s metodou funkce application.worksheet.

Funkce RIGHT v aplikaci Excel VBA se používá k extrakci znaků z pravé strany zadaných textových hodnot. V aplikaci Excel máme mnoho textových funkcí pro práci s textovými daty. Některé z užitečných funkcí jsou funkce LEN, LEFT, RIGHT, MID pro extrahování znaků z textových hodnot. Běžným příkladem použití těchto funkcí je extrahování jména a příjmení odděleně od celého jména.

Rovný vzorec je také v listu. Ve VBA se při přístupu k funkci VBA RIGHT musíme spoléhat na třídu funkcí listu; spíše máme vestavěnou funkci RIGHT i ve VBA.

Nyní se podívejte na níže uvedenou syntaxi vzorce VBA RIGHT.

  • Řetězec: Toto je naše hodnota a z této hodnoty se snažíme extrahovat znaky z pravé strany řetězce.
  • Délka: Z dodaného řetězce, kolik znaků potřebujeme. Pokud potřebujeme čtyři znaky z pravé strany, můžeme argument dodat jako 4.

Pokud je například řetězec „Mobilní telefon“ a chceme-li extrahovat pouze slovo „Telefon“, můžeme zadat argument jako níže.

VPRAVO („Mobilní telefon“, 5)

Důvod, proč jsem zmínil 5, protože slovo „Telefon“ má 5 písmen. V další části článku uvidíme, jak jej můžeme použít ve VBA.

Příklady funkce Excel VBA Right

Následuje ukázka správných funkcí VBA Excel.

Příklad č. 1

Ukážu vám jednoduchý příklad k zahájení řízení. Předpokládejme, že máte řetězec „New York“, a pokud chcete zprava extrahovat 3 znaky, zadejte kód podle následujících kroků.

Krok 1: Deklarujte proměnnou jako řetězec VBA.

Kód:

Sub Right_Example1 () Dim k As String End Sub

Krok 2: Nyní této proměnné přiřadíme hodnotu pomocí funkce RIGHT.

Kód:

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

Krok 3: První argument je řetězec a náš řetězec pro tento příklad je „New York“.

Kód:

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

Krok 4: Další krok je, kolik znaků potřebujeme z dodaného řetězce. V tomto příkladu potřebujeme 3 znaky.

Kód:

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

Krok 5: Musíme se vypořádat se 2 argumenty, takže jsme hotoví. Nyní přiřaďte hodnotu této proměnné do pole se zprávou ve VBA.

Kód:

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

Spusťte kód pomocí klávesy F5 nebo ručně a zobrazte výsledek v okně se zprávou.

Ve slově „New York“ z pravé strany jsou 3 znaky „ork“.

Nyní změním délku ze 3 na 4, abych získal plnou hodnotu.

Kód:

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

Spusťte tento kód ručně nebo pomocí klávesy F5. Potom dostaneme „York“.

Příklad č. 2

Nyní se podívejte na další příklad, tentokrát považujte hodnotu řetězce za „Michaela Clarka“.

Pokud zadáte délku 6, dostaneme jako výsledek „Clarke“.

Kód:

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

Spusťte tento kód pomocí klávesy F5 nebo ručně, abyste viděli výsledek.

Funkce Dynamic Right v aplikaci Excel VBA

If you observe our previous two examples, we have supplied the length argument numbers manually. But this is not the right process to do the job.

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”) returns 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 each string, we can use this. Once we find the space character position, we need to minus that from the 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 the 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, "") Buňky (a, 2) .Value = Right (Buňky (a, 1), L - S) Další a End Sub

Výsledek tohoto kódu je následující.

Zajímavé články...