VBA Right-functie (voorbeelden) - Stapsgewijze handleiding voor Excel VBA Right

Juiste functie in VBA Excel

Right-functie is hetzelfde als in zowel de werkbladfunctie als in VBA, het gebruik van deze functie is dat het ons de substring van een bepaalde string geeft, maar de zoekopdracht wordt gedaan van rechts naar links van de string, dit is een type stringfunctie in VBA gebruikt met application.worksheet-functiemethode.

RIGHT-functie in Excel VBA wordt gebruikt om tekens uit de rechterkant van de geleverde tekstwaarden te extraheren. In Excel hebben we veel tekstfuncties om met op tekst gebaseerde gegevens om te gaan. Enkele van de handige functies zijn LEN, LINKS, RECHTS, MIDDEN-functie om tekens uit de tekstwaarden te extraheren. Het gebruikelijke voorbeeld van het gebruik van deze functies is het apart extraheren van de voornaam en achternaam uit de volledige naam.

De RECHTER-formule staat ook in het werkblad. In VBA moeten we vertrouwen op de werkbladfunctieklasse om toegang te krijgen tot de VBA RIGHT-functie; in plaats daarvan hebben we ook de ingebouwde RIGHT-functie in VBA.

Bekijk nu de onderstaande syntaxis van de VBA RIGHT-formule.

  • String: dit is onze waarde en uit deze waarde proberen we de tekens aan de rechterkant van de string te extraheren.
  • Lengte: van de meegeleverde string, hoeveel karakters we nodig hebben. Als we vier tekens van de rechterkant nodig hebben, kunnen we het argument 4 opgeven.

Als de tekenreeks bijvoorbeeld "Mobiele telefoon" is en we alleen het woord "Telefoon" willen extraheren, kunnen we het argument opgeven zoals hieronder.

RIGHT ("Mobiele telefoon", 5)

De reden waarom ik 5 noemde omdat het woord "Telefoon" 5 letters bevat. In het volgende gedeelte van het artikel zullen we zien hoe we het in VBA kunnen gebruiken.

Voorbeelden van Excel VBA Right-functie

Hieronder volgen de voorbeelden van Right Function VBA Excel.

Voorbeeld 1

Ik zal u een eenvoudig voorbeeld laten zien om de procedure te starten. Stel dat u de tekenreeks "New York" hebt, en als u drie tekens van rechts wilt extraheren, volgt u de onderstaande stappen om de code te schrijven.

Stap 1: Declareer de variabele als VBA-string.

Code:

Sub Right_Example1 () Dim k As String End Sub

Stap 2: Nu zullen we voor deze variabele de waarde toewijzen door de functie RECHTS toe te passen.

Code:

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

Stap 3: Het eerste argument is String en onze string voor dit voorbeeld is 'New York'.

Code:

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

Stap 4: De volgende stap is hoeveel tekens we nodig hebben van de meegeleverde string. In dit voorbeeld hebben we 3 karakters nodig.

Code:

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

Stap 5: We hebben 2 argumenten om mee om te gaan, dus we zijn klaar. Wijs nu de waarde van deze variabele toe in het berichtvenster in VBA.

Code:

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

Voer de code uit met behulp van de F5-toets of handmatig en bekijk het resultaat in een berichtvenster.

In het woord 'New York' aan de rechterkant zijn drie tekens 'ork'.

Nu zal ik de lengte veranderen van 3 naar 4 om de volledige waarde te krijgen.

Code:

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

Voer deze code handmatig uit of gebruik de F5-toets. Dan krijgen we 'York'.

Voorbeeld 2

Bekijk nu nog een voorbeeld, beschouw deze keer de tekenreekswaarde als 'Michael Clarke'.

Als u de lengte 6 opgeeft, krijgen we als resultaat “Clarke”.

Code:

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

Voer deze code uit met behulp van de F5-toets of handmatig om het resultaat te zien.

Dynamic Right-functie in 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 tot 5 L = Len (Cells (a, 1) .Value) S = InStr (1, Cells (a, 1 ) .Value, "") Cellen (a, 2) .Value = Right (Cellen (a, 1), L - S) Next a End Sub

Het resultaat van deze code is als volgt.

Interessante artikelen...