VBA Sağ İşlevi (Örnekler) - Excel VBA Sağa Adım Adım Kılavuz

VBA Excel'de Sağ Fonksiyon

Sağ İşlevi, hem çalışma sayfası işlevinde hem de VBA'da olduğu gibi aynıdır, bu işlevin kullanımı bize belirli bir dizeden alt dizeyi vermesidir, ancak arama dizenin sağından soluna yapılır, bu VBA'da bir dizi işlev türüdür application.worksheet işlevi yöntemi ile kullanılır.

Excel VBA'daki SAĞ İşlevi, sağlanan metin değerlerinin sağ tarafındaki karakterleri ayıklamak için kullanılır. Excel'de, metin tabanlı verilerle ilgilenmek için birçok metin işlevimiz var. Kullanışlı işlevlerden bazıları, metin değerlerinden karakterleri çıkarmak için UZUNLUK, SOL, SAĞ, ORTA işlevidir. Bu işlevleri kullanmanın yaygın örneği, adı ve soyadı tam addan ayrı olarak çıkarmaktır.

DOĞRU formül de çalışma sayfasında var. VBA'da, VBA RIGHT işlevine erişmek için çalışma sayfası işlev sınıfına güvenmemiz gerekir; bunun yerine VBA'da yerleşik SAĞ işlevi de var.

Şimdi VBA RIGHT formülünün aşağıdaki sözdizimine bir göz atın.

  • String: Bu bizim değerimiz ve bu değerden karakterleri dizenin sağ tarafından çıkarmaya çalışıyoruz.
  • Uzunluk: Verilen dizeden kaç karaktere ihtiyacımız var. Sağ taraftan dört karaktere ihtiyacımız varsa, argümanı 4 olarak sağlayabiliriz.

Örneğin, dizge "Cep Telefonu" ise ve yalnızca "Telefon" kelimesini çıkarmak istiyorsak, aşağıdaki gibi argümanı sağlayabiliriz.

SAĞ ("Cep Telefonu" 5)

5'ten bahsetmemin nedeni "Telefon" kelimesinin içinde 5 harf olması. Makalenin bir sonraki bölümünde VBA'da nasıl kullanabileceğimizi göreceğiz.

Excel VBA Sağ Fonksiyonu Örnekleri

Aşağıdakiler Sağ İşlev VBA Excel'in örnekleridir.

Örnek 1

İşlemlere başlamak için size basit bir örnek göstereceğim. "New York" dizesine sahip olduğunuzu varsayın ve sağdan 3 karakter çıkarmak istiyorsanız, kodu yazmak için aşağıdaki adımları izleyin.

Adım 1: Değişkeni VBA Dizesi olarak bildirin.

Kod:

Alt Sağ_Örnek1 () Dim k As String End Sub

Adım 2: Şimdi bu değişken için RIGHT fonksiyonunu uygulayarak değeri atayacağız.

Kod:

Alt Sağ_Örnek1 () Dim k As String k = Right (End Sub

3. Adım: İlk bağımsız değişken String'dir ve bu örnek için dizemiz "New York" dur.

Kod:

Alt Sağ_Örnek1 () Dim k As String k = Sağ ("New York", End Sub

Adım 4: Sıradaki, sağlanan dizeden kaç karaktere ihtiyacımız olduğudur. Bu örnekte 3 karaktere ihtiyacımız var.

Kod:

Alt Sağ_Örnek1 () Dim k As String k = Right ("New York", 3) End Sub

Adım 5: Halletmemiz gereken 2 argüman var, yani işimiz bitti. Şimdi bu değişkenin değerini VBA'daki mesaj kutusuna atayın.

Kod:

Alt Sağ_Örnek1 () Dim k As String k = Right ("New York", 3) MsgBox k End Sub

Kodu F5 tuşunu kullanarak veya manuel olarak çalıştırın ve sonucu bir mesaj kutusunda görün.

Sağ taraftaki "New York" kelimesinde 3 karakter "ork" dur.

Şimdi tam değeri elde etmek için uzunluğu 3'ten 4'e değiştireceğim.

Kod:

Alt Sağ_Örnek1 () Dim k As String k = Right ("New York", 4) MsgBox k End Sub

Bu kodu manuel olarak veya F5 tuşunu kullanarak çalıştırın. Sonra "York" u alacağız.

Örnek 2

Şimdi bir örneğe daha göz atın, bu sefer dize değerini "Michael Clarke" olarak düşünün.

Uzunluğu 6 olarak verirseniz, sonuç olarak "Clarke" alacağız.

Kod:

Alt Sağ_Örnek1 () Dim k As String k = Right ("Michael Clarke", 6) MsgBox k End Sub

Sonucu görmek için bu kodu F5 tuşunu kullanarak veya manuel olarak çalıştırın.

Excel VBA'da Dinamik Sağ Fonksiyon

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:

Alt Sağ_Örnek4 () Dim k As String Dim L As String Dim S As String Dim a As Tamsayı İçin a = 2 ila 5 L = Len (Hücreler (a, 1) .Değer) S = InStr (1, Hücreler (a, 1) ) .Değer, "") Hücreler (a, 2) .Değer = Sağ (Hücreler (a, 1), L - S) Sonraki a End Sub

Bu kodun sonucu aşağıdaki gibidir.

Ilginç makaleler...