Funkce VBA TRIM - Jak používat funkci Excel VBA TRIM?

Funkce Excel VBA TRIM

VBA TRIM je kategorizován pod řetězcovými a textovými funkcemi, tato funkce je funkcí listu ve VBA a podobně jako odkaz na list se tato funkce používá k oříznutí nebo odstranění nežádoucích mezer z řetězce, trvá jediný argument, kterým je vstupní řetězec a vrátí výstup jako řetězec.

Existuje několik situací, kdy stahujeme data z online serverů, a čelíme situaci neorganizovaných dat a nežádoucích mezer v buňce. Nakládání s nepotřebnými prostory je velmi bolestivé. V běžných funkcích listu máme funkci zvanou TRIM v aplikaci Excel, abychom se zbavili „Vedoucích prostorů, Koncových prostorů a meziprostoru.“ Ve VBA také máme funkci nazvanou TRIM, která funguje přesně stejně jako funkce aplikace Excel, ale existuje jen jeden nepatrný rozdíl. Uvidíme, že v tomto článku o něco později.

Co dělá funkce Trim?

TRIM je funkce řetězce, která odstraňuje mezery v aplikaci Excel z vybrané buňky. V prostorech máme 3 typy „Přední mezery, koncové mezery a meziprostor.“

Leading Space není nic, ale pokud v buňce začne nějaká hodnota, pokud je v ní nějaký prostor, bude se jmenovat Leading Space.

Trailing Space není nic jiného než po konci hodnoty v buňce, pokud je v ní nějaký prostor, pak se nazývá Trailing Space.

Meziprostor není po konci každého slova nic; v ideálním případě bychom měli mít jeden znak mezery. Cokoli více než jedna vesmírná postava se nazývá Meziprostor.

Abychom překonali všechny tyto druhy problémů, máme funkci nazvanou TRIM.

Syntax

Řetězec: Jaký řetězec nebo hodnotu chcete oříznout? Může to být odkaz na buňku i přímý přísun hodnoty do vzorce.

Příklady

Příklad č. 1

Nyní uvidíme, jak odstranit přední mezery v aplikaci Excel pomocí funkce TRIM. Postupujte podle níže uvedených kroků

Krok 1: Vytvořte název makra a deklarujte proměnnou jako řetězec.

Kód:

Sub Trim_Example1 () Dim k As String End Sub

Krok 2: Nyní pro tuto deklarovanou proměnnou přiřaďte hodnotu pomocí TRIM.

Kód:

Sub Trim_Example1 () Dim k As String k = Trim (End Sub

Krok 3: U tohoto typu řetězce je slovo v uvozovkách „Vítejte ve VBA“.

Kód:

Sub Trim_Example1 () Dim k As String k = Trim ("Vítejte ve VBA") End Sub

Krok 4: Nyní zobrazte výsledek této proměnné v okně se zprávou.

Kód:

Sub Trim_Example1 () Dim k As String k = Trim ("Vítejte ve 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.
  • Funkce oříznutí aplikace Excel dokáže odstranit všechny druhy mezer.

Zajímavé články...