VBA-oikea toiminto (esimerkkejä) Vaiheittainen opas Excel VBA Right -sovellukseen

Oikea toiminto VBA Excelissä

Oikea toiminto on sama kuin molemmissa laskentataulukko-funktioissa ja VBA: ssa, tämän funktion käyttö on se, että se antaa meille alamerkkijonon tietystä merkkijonosta, mutta haku tehdään merkkijonosta oikealta vasemmalle, tämä on eräänlainen merkkijonofunktio VBA: ssa käytetään application.worksheet-toimintomenetelmän kanssa.

OIKEA-funktiota Excel VBA: ssa käytetään merkkien purkamiseen toimitettujen tekstiarvojen oikealta puolelta. Excelissä meillä on monia tekstitoimintoja tekstipohjaisen datan käsittelemiseksi. Joitakin hyödyllisiä toimintoja ovat LEN, VASEN, OIKEA, MID-toiminto merkkien poimimiseksi tekstiarvoista. Yleinen esimerkki näiden toimintojen käytöstä on etunimen ja sukunimen purkaminen erikseen koko nimestä.

OIKEA kaava on myös siellä laskentataulukossa. VBA: ssa meidän on luotettava laskentataulukon toimintoluokkaan päästäksesi VBA OIKEA-toimintoon; pikemminkin meillä on sisäänrakennettu OIKEA-toiminto myös VBA: ssa.

Katsokaa nyt VBA RIGHT -kaavan alla olevaa syntaksia.

  • Merkkijono: Tämä on arvomme, ja tästä arvosta yritämme poimia merkit merkkijonon oikealta puolelta.
  • Pituus: Mukana toimitetusta merkkijonosta kuinka monta merkkiä tarvitsemme. Jos tarvitsemme neljä merkkiä oikealta puolelta, voimme antaa argumentin 4.

Esimerkiksi, jos merkkijono on "Matkapuhelin" ja jos haluamme purkaa vain sanan "Puhelin", voimme toimittaa argumentin kuten alla.

OIKEA ("Matkapuhelin", 5)

Syy miksi mainitsin 5, koska sanassa "Puhelin" on 5 kirjainta. Artikkelin seuraavassa osassa näemme, kuinka voimme käyttää sitä VBA: ssa.

Esimerkkejä Excel VBA -oikeustoiminnosta

Seuraavassa on esimerkkejä oikean toiminnon VBA Excelistä.

Esimerkki 1

Esitän sinulle yksinkertaisen esimerkin menettelyn aloittamiseksi. Oletetaan, että sinulla on merkkijono "New York", ja jos haluat poimia 3 merkkiä oikealta, kirjoita koodi noudattamalla seuraavia ohjeita.

Vaihe 1: Ilmoita muuttuja VBA-merkkijonoksi.

Koodi:

Oikea ala_esimerkki1 () Himmennä merkkijonon loppuosana

Vaihe 2: Nyt tälle muuttujalle määritämme arvon soveltamalla OIKEA-toimintoa.

Koodi:

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

Vaihe 3: Ensimmäinen argumentti on merkkijono, ja tämän esimerkin merkkijono on "New York".

Koodi:

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

Vaihe 4: Seuraavassa on kuinka monta merkkiä tarvitsemme toimitetusta merkkijonosta. Tässä esimerkissä tarvitaan 3 merkkiä.

Koodi:

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

Vaihe 5: Meillä on kaksi argumenttia käsiteltäväksi, joten olemme valmiit. Määritä nyt tämän muuttujan arvo VBA: n viestiruutuun.

Koodi:

Oikea_esimerkki1 () Dim k As String k = Oikea ("New York", 3) MsgBox k End Sub

Suorita koodi F5-näppäimellä tai manuaalisesti ja katso tulos viestiruudussa.

Sanassa "New York" oikealla puolella 3 merkkiä ovat "ork".

Nyt vaihdan pituuden 3: sta 4: een saadaksesi täyden arvon.

Koodi:

Oikea_esimerkki_esimerkki1 () Dim k As String k = Oikea ("New York", 4) MsgBox k End Sub

Suorita tämä koodi manuaalisesti tai F5-näppäimellä. Sitten saamme “Yorkin”.

Esimerkki 2

Katso nyt vielä yksi esimerkki, tällä kertaa pidä merkkijonon arvona "Michael Clarke".

Jos ilmoitat pituudeksi 6, tuloksena on "Clarke".

Koodi:

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

Suorita tämä koodi F5-näppäimellä tai manuaalisesti nähdäksesi tuloksen.

Dynaaminen oikea toiminto Excel VBA: ssa

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:

Oikea_esimerkki4 () Dim k merkkijonona Dim L kuten merkkijono Dim S merkkijonona Dim a kokonaislukuna a = 2 - 5 L = Len (solut (a, 1). Arvo) S = InStr (1, solut (a, 1) ) .Arvo, "") Solut (a, 2) .Arvo = Oikea (Solut (a, 1), L - S) Seuraava loppuosa

Tämän koodin tulos on seuraava.

Mielenkiintoisia artikkeleita...