Prawa funkcja VBA (przykłady) - Przewodnik krok po kroku do Excel VBA Right

Właściwa funkcja w VBA Excel

Prawa funkcja jest taka sama jak w funkcji arkusza i VBA, użycie tej funkcji polega na tym, że daje nam podciąg z danego ciągu, ale wyszukiwanie odbywa się od prawej do lewej strony ciągu, jest to typ funkcji ciągu w VBA używane z metodą funkcji application.worksheet.

PRAWO Funkcja w Excel VBA służy do wyodrębniania znaków z prawej strony podanych wartości tekstowych. W programie Excel mamy wiele funkcji tekstowych do obsługi danych tekstowych. Niektóre z przydatnych funkcji to LEN, LEFT, RIGHT, MID do wyodrębniania znaków z wartości tekstowych. Typowym przykładem użycia tych funkcji jest wyodrębnienie imienia i nazwiska niezależnie od imienia i nazwiska.

W arkuszu znajduje się również formuła PRAWA. W języku VBA musimy polegać na klasie funkcji arkusza, aby uzyskać dostęp do funkcji VBA RIGHT; raczej mamy wbudowaną funkcję PRAWO w VBA.

Spójrz teraz na poniższą składnię formuły VBA RIGHT.

  • Ciąg: To jest nasza wartość iz tej wartości próbujemy wyodrębnić znaki z prawej strony ciągu.
  • Długość: z dostarczonego ciągu , ile znaków potrzebujemy. Jeśli potrzebujemy czterech znaków z prawej strony, możemy podać argument jako 4.

Na przykład, jeśli ciąg to „Telefon komórkowy” i jeśli chcemy wyodrębnić tylko słowo „Telefon”, możemy podać argument podobny do poniższego.

PRAWO („Telefon komórkowy” 5)

Powód, dla którego wspomniałem o 5, ponieważ słowo „telefon” zawiera 5 liter. W kolejnej części artykułu zobaczymy, jak możemy go wykorzystać w VBA.

Przykłady prawej funkcji Excel VBA

Poniżej znajdują się przykłady prawej funkcji VBA Excel.

Przykład 1

Pokażę Ci prosty przykład na rozpoczęcie postępowania. Załóżmy, że masz ciąg „Nowy Jork” i jeśli chcesz wyodrębnić 3 znaki z prawej strony, wykonaj poniższe czynności, aby napisać kod.

Krok 1: Zadeklaruj zmienną jako ciąg VBA.

Kod:

Sub Right_Example1 () Dim k As String End Sub

Krok 2: Teraz dla tej zmiennej przypiszemy wartość, stosując funkcję PRAWO.

Kod:

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

Krok 3: pierwszy argument to String, a nasz ciąg w tym przykładzie to „Nowy Jork”.

Kod:

Sub Right_Example1 () Dim k As String k = Right ("Nowy Jork", End Sub

Krok 4: Następnie podajemy, ile znaków potrzebujemy z podanego ciągu. W tym przykładzie potrzebujemy 3 znaków.

Kod:

Sub Right_Example1 () Dim k As String k = Right ("Nowy Jork", 3) End Sub

Krok 5: Mamy 2 argumenty do rozwiązania, więc gotowe. Teraz przypisz wartość tej zmiennej w oknie komunikatu w VBA.

Kod:

Sub Right_Example1 () Dim k As String k = Right ("Nowy Jork", 3) MsgBox k End Sub

Uruchom kod za pomocą klawisza F5 lub ręcznie i zobacz wynik w oknie komunikatu.

W słowie „Nowy Jork” z prawej strony 3 znaki to „ork”.

Teraz zmienię długość z 3 na 4, aby uzyskać pełną wartość.

Kod:

Sub Right_Example1 () Dim k As String k = Right ("Nowy Jork", 4) MsgBox k End Sub

Uruchom ten kod ręcznie lub za pomocą klawisza F5. Wtedy otrzymamy „York”.

Przykład nr 2

Spójrzmy teraz na jeszcze jeden przykład, tym razem rozważmy wartość ciągu jako „Michael Clarke”.

Jeśli podasz długość jako 6, jako wynik otrzymamy „Clarke”.

Kod:

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

Uruchom ten kod za pomocą klawisza F5 lub ręcznie, aby zobaczyć wynik.

Dynamiczna prawostronna funkcja w 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, "") Cells (a, 2) .Value = Right (Cells (a, 1), L - S) Next a End Sub

Wynik tego kodu jest następujący.

Interesujące artykuły...