Excel VBA TRIM-Funktion
VBA TRIM ist unter den Zeichenfolgen- und Textfunktionen kategorisiert. Diese Funktion ist eine Arbeitsblattfunktion in VBA. Ähnlich wie die Arbeitsblattreferenz wird diese Funktion zum Trimmen oder Entfernen unerwünschter Leerzeichen aus einer Zeichenfolge verwendet. Es wird ein einzelnes Argument verwendet, das eine Eingabezeichenfolge ist und gibt eine Ausgabe als Zeichenfolge zurück.
Es gibt verschiedene Situationen, in denen wir die Daten von Online-Servern herunterladen und unorganisierte Daten und unerwünschte Speicherplätze in der Zelle haben. Der Umgang mit unnötigen Räumen ist sehr schmerzhaft. In regulären Arbeitsblattfunktionen haben wir eine Funktion namens TRIM in Excel, um "Leading Spaces, Trailing Spaces und Zwischenräume" zu entfernen. In VBA haben wir auch eine Funktion namens TRIM, die genauso funktioniert wie die Excel-Funktion, aber es gibt nur einen kleinen Unterschied. Wir werden das etwas später in diesem Artikel sehen.

Was macht die Trimmfunktion?
TRIM ist eine Zeichenfolgenfunktion, die Leerzeichen in Excel aus der ausgewählten Zelle entfernt. In Räumen gibt es drei Typen: "Führende Räume, nachfolgende Räume und Zwischenräume".
Leading Space ist nichts, aber bevor ein Wert in der Zelle beginnt, wird er als Leading Space bezeichnet , wenn ein Leerzeichen vorhanden ist .

Trailing Space ist nichts anderes als nach dem Ende des Werts in der Zelle, wenn Leerzeichen vorhanden sind, wird es als Trailing Space bezeichnet.

Zwischenraum ist nichts nach dem Ende eines jeden Wortes; Idealerweise sollten wir einen Leerzeichen haben. Alles, was mehr als ein Leerzeichen enthält, wird als Zwischenraum bezeichnet.

Um all diese Probleme zu lösen, haben wir eine Funktion namens TRIM.
Syntax

Zeichenfolge : Welche Zeichenfolge oder welchen Wert möchten Sie zuschneiden? Dies kann sowohl eine Zellreferenz als auch eine direkte Wertversorgung der Formel sein.
Beispiele
Beispiel 1
Jetzt werden wir sehen, wie führende Leerzeichen in Excel mit der TRIM-Funktion entfernt werden. Befolgen Sie die folgenden Schritte
Schritt 1: Erstellen Sie einen Makronamen und deklarieren Sie eine Variable als String.
Code:
Sub Trim_Example1 () Dim k As String End Sub

Schritt 2: Weisen Sie nun für diese deklarierte Variable mithilfe von TRIM einen Wert zu.
Code:
Sub Trim_Example1 () Dim k As String k = Trim (End Sub

Schritt 3: Für diesen Zeichenfolgentyp das Wort in doppelten Anführungszeichen als "Willkommen bei VBA".
Code:
Sub Trim_Example1 () Dim k As String k = Trim ("Willkommen bei VBA") End Sub

Schritt 4: Zeigen Sie nun das Ergebnis dieser Variablen im Meldungsfeld an.
Code:
Sub Trim_Example1 () Dim k As String k = Trim ("Willkommen bei VBA") MsgBox k End Sub

Step 5: Run this code using the F5 key or run manually. We should get a correct string in the message box.

Output:

So, the TRIM function removed all the leading spaces we have entered into the string value. Now the result is a correct sentence, i.e., “Welcome to VBA.”
Example #2
Now take a look at one more example to remove trailing spaces. Take the same word but this time, enter trailing spaces.
“Welcome to VBA. “
Code:
Sub Trim_Example2() Dim k As String k = Trim("Welcome to VBA ") MsgBox k End Sub

Run this code manually or through the F5 key Trim function removes trailing spaces as well.

Output:

Example #3 - Clean the Data in Cells
Now we will see the practical example of cleaning the data in excel cells. Assume you have a data set like the below on in your excel sheet.

When we have more than one cell data, we need to write different codes. Follow the below steps to write code.
Step 1: Declare a variable as “Range.”
Code:
Sub Trim_Example3() Dim MyRange As Range End Sub

Step 2: Since the range data type is an object, we need to set the range first. So set the range as “selection.”
Code:
Sub Trim_Example3() Dim MyRange As Range Set MyRange = Selection End Sub

Note: MyRange = Selection means whatever the cells I select becomes a range. So in that range, TRIM removes unwanted spaces.
Step 3: Now, using For Each VBA Loop, apply the TRIM function.
Code:
Sub Trim_Example3() Dim MyRange As Range Set MyRange = Selection For Each cell In MyRange cell.Value = Trim(cell) Next End Sub

Step 4: Now go back to the worksheet. Go to insert and draw a rectangular shape.

Step 5: Add a word to the inserted rectangular shape as click here to clean the data.

Step 6: Right-click on the rectangular shape and select the assigned macro.

Step 7: Now, the macro names box will open. Select the name of the macro we just created. Click on Ok.
Step 8: Now select the range where we need to clean the data.

Step 9: After selecting the range, click on the assigned macro rectangular box. It will clean the data.

So we got cleaned value, i.e., after removing leading & trailing spaces.
Difference Between Excel & VBA Trim Function
The only difference between them is VBA function cannot remove in between spaces while the worksheet function in VBA does. For example, take a look at the below example.

In cell A3, we have in-between spaces. If select and click on the rectangular shape, it will only remove trailing & leading spaces, not in-between spaces.

To overcome this problem, we need to use the trim function as a worksheet function in the code.
Code:
Sub Trim_Example3() Dim MyRange As Range Set MyRange = Selection For Each cell In MyRange cell.Value = WorksheetFunction.Trim(cell) Next End Sub

Here we have used a worksheet function, not a VBA function. This will remove all kinds of spaces.
Things to Remember
- VBA Trim & Worksheet Trim syntax is exactly the same.
- It can only remove leading & trailing spaces, not in-between spaces.
- Die Excel-Trimmfunktion kann alle Arten von Leerzeichen entfernen.