VBA parem funktsioon (näited) Exceli VBA õigesti juhised

Lang L: none (table-of-contents)

Parem funktsioon VBA Excelis

Parempoolne funktsioon on sama mis nii töölehe funktsioonis kui ka VBA-s, selle funktsiooni kasutamine on see, et see annab meile antud stringist alamstringi, kuid otsing toimub stringist paremalt vasakule, see on VBA-s stringifunktsiooni tüüp kasutatakse koos application.heetheet funktsiooni meetodiga.

RIGHT Exceli funktsiooni VBA kasutatakse märkide eraldamiseks kaasasolevate tekstiväärtuste paremast küljest. Excelis on meil tekstipõhiste andmetega tegelemiseks palju tekstifunktsioone. Mõned kasulikud funktsioonid on funktsioon LEN, LEFT, RIGHT, MID, et eraldada tekstiväärtustest märke. Nende funktsioonide levinud näide on eesnime ja perekonnanime eraldamine täisnimest eraldi.

PAREM valem on olemas ka töölehel. VBA-s peame funktsioonile VBA RIGHT pääsemiseks tuginema töölehe funktsiooniklassile; pigem on meil sisseehitatud funktsioon PAREM ka VBA-s.

Nüüd heitke pilk VBA RIGHT valemi allpool olevale süntaksile.

  • String: See on meie väärtus ja sellest väärtusest püüame tähemärki stringi paremast küljest välja tõmmata.
  • Pikkus: kaasasolevast stringist, kui palju märke vajame. Kui vajame parempoolsest küljest nelja märki, võime argumendi esitada 4-na.

Näiteks kui string on „Mobiiltelefon” ja kui tahame välja tõmmata ainult sõna „Telefon”, siis võime esitada argumendi nagu allpool.

PAREM („Mobiiltelefon”, 5)

Põhjus, miks ma mainisin 5, kuna sõnal "Telefon" on 5 tähte. Artikli järgmises jaotises näeme, kuidas saame seda VBA-s kasutada.

Exceli VBA parempoolse funktsiooni näited

Järgnevalt on toodud parema funktsiooni VBA Exceli näited.

Näide 1

Näitan teile menetluse alustamiseks lihtsat näidet. Oletame, et teil on string „New York” ja kui soovite 3 tähemärki paremalt välja tõmmata, järgige koodi kirjutamiseks allolevaid juhiseid.

1. samm: kuulutage muutuja VBA-stringiks.

Kood:

Parempoolne_näide1 () Dim k String End Sub

2. samm: nüüd määrame selle muutuja jaoks väärtuse, rakendades funktsiooni PAREM.

Kood:

Parempoolne_näide1 () Dim k As String k = Right (End Sub

3. samm: esimene argument on String ja meie selle näite string on „New York”.

Kood:

Parempoolne_näide1 () Dim k As String k = Right ("New York", Sub Sub

4. samm: Järgmine on see, mitu tähte vajame kaasasolevast stringist. Selles näites vajame 3 tähemärki.

Kood:

Parempoolne_näide1 () Dim k As String k = Parem ("New York", 3) Lõpp alam

5. samm: meil on 2 argumenti, millega tuleb tegeleda, nii et oleme sellega valmis. Nüüd määrake selle muutuja väärtus VBA teatekastis.

Kood:

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

Käivitage kood klahviga F5 või käsitsi ja vaadake tulemust teatekastis.

Sõna „New York” paremal küljel on 3 tähemärki „ork”.

Nüüd muudan täisväärtuse saamiseks pikkuse 3-lt 4-le.

Kood:

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

Käivitage see kood käsitsi või klahvi F5 abil. Siis saame "York".

Näide 2

Vaadake nüüd veel ühte näidet. Seekord loe stringi väärtuseks “Michael Clarke”.

Kui sisestate pikkuseks 6, saame tulemuseks “Clarke”.

Kood:

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

Tulemuse nägemiseks käivitage see kood klahviga F5 või käsitsi.

Dünaamiline parempoolne funktsioon Exceli VBA-s

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:

Parempoolne_näide4 () Dim k stringina Dim L stringina Dim S stringina Dim a täisarvuna a = 2 kuni 5 L = Len (lahtrid (a, 1). Väärtus) S = InStr (1, lahtrid (a, 1 ) .Väärtus, "") Lahtrid (a, 2). Väärtus = Parem (Lahtrid (a, 1), L - S) Järgmine lõpuosa

Selle koodi tulemus on järgmine.

Huvitavad Artiklid...