Remove Spaces in the beginning of the cell and the end (TRIM)

Andresuru

New Member
Joined
Sep 6, 2021
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hello Team,

I was wondering if someone can help with a VBA code, I dont have experience using trim to remove blank spaces at the beginning and the end of the cells.

Expectation: Basically i need to remove blank spaces (Beginning & the end) in each cell with strings in column B but only the cells that are not empty until last row that has values.
I guess the loop needs to be from Activecell to LastRow with values.

I appreciate a lot if you can help since I need to add this VBA code to another macro so data can be look friendly and well.
Please let me know if you need an excel file or photo about the expectation.

Note: I already checked other threads but I cannot find it, appreciate help me on this :)
Regards
 
The code you posted has errors in it. I tried to fix them and made some modifications so the user can first select an entire column, then run the code and it will trim from row 2 to the last filled row in the selected column.
VBA Code:
Sub TRIMPRO()
Dim V As Variant, i As Long
With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    .AskToUpdateLinks = False
    V = Range(Cells(2, ActiveCell.Column), Cells(Rows.Count, ActiveCell.Column).End(xlUp)).Value
        For i = 1 To UBound(V, 1)
            If V(i, 1) <> "" Then
                V(i, 1) = Trim(V(i, 1))
            End If
        Next i
        Range(Cells(2, ActiveCell.Column), Cells(Rows.Count, ActiveCell.Column).End(xlUp)).Value = V
.ScreenUpdating = True
.DisplayAlerts = True
.AskToUpdateLinks = True
End With
End Sub
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
The code you posted has errors in it. I tried to fix them and made some modifications so the user can first select an entire column, then run the code and it will trim from row 2 to the last filled row in the selected column.
VBA Code:
Sub TRIMPRO()
Dim V As Variant, i As Long
With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    .AskToUpdateLinks = False
    V = Range(Cells(2, ActiveCell.Column), Cells(Rows.Count, ActiveCell.Column).End(xlUp)).Value
        For i = 1 To UBound(V, 1)
            If V(i, 1) <> "" Then
                V(i, 1) = Trim(V(i, 1))
            End If
        Next i
        Range(Cells(2, ActiveCell.Column), Cells(Rows.Count, ActiveCell.Column).End(xlUp)).Value = V
.ScreenUpdating = True
.DisplayAlerts = True
.AskToUpdateLinks = True
End With
End Sub

I have been spending like 2 hours today trying to get results.
Wonderful , works great. Thanks Again
 
Upvote 0
The code you posted has errors in it. I tried to fix them and made some modifications so the user can first select an entire column, then run the code and it will trim from row 2 to the last filled row in the selected column.
VBA Code:
Sub TRIMPRO()
Dim V As Variant, i As Long
With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    .AskToUpdateLinks = False
    V = Range(Cells(2, ActiveCell.Column), Cells(Rows.Count, ActiveCell.Column).End(xlUp)).Value
        For i = 1 To UBound(V, 1)
            If V(i, 1) <> "" Then
                V(i, 1) = Trim(V(i, 1))
            End If
        Next i
        Range(Cells(2, ActiveCell.Column), Cells(Rows.Count, ActiveCell.Column).End(xlUp)).Value = V
.ScreenUpdating = True
.DisplayAlerts = True
.AskToUpdateLinks = True
End With
End Sub

Hi @JoeMo , Checking this VBA code with other person it looks the VBA code is not working with excel 365 32 bits only for 64 bit.
How should be the code for version 32 bit?

Appreciate your response
Thanks
 
Upvote 0
Hi @JoeMo , Checking this VBA code with other person it looks the VBA code is not working with excel 365 32 bits only for 64 bit.
How should be the code for version 32 bit?

Appreciate your response
Thanks
I'm using 32 bit excel with MS 365 and the code is working. Below from File>Account>About Excel:
Microsoft® Excel® for Microsoft 365 MSO (Version 2201 Build 16.0.14827.20028) 32-bit

Can you tell me what exactly is "not working" for you? Can't compile or you get a run time error, or what ....?
 
Upvote 0
I'm using 32 bit excel with MS 365 and the code is working. Below from File>Account>About Excel:
Microsoft® Excel® for Microsoft 365 MSO (Version 2201 Build 16.0.14827.20028) 32-bit

Can you tell me what exactly is "not working" for you? Can't compile or you get a run time error, or what ....?

Hi @JoeMo ,

Apologies for the delayed,

The error is in the image attached.

Regards
Andres
 

Attachments

  • TRIM Error.JPG
    TRIM Error.JPG
    60.4 KB · Views: 10
Upvote 0
Do you have a module that you named "Trim"? If so, that is the problem (you should not use VBA keywords for the names of modules, variable, functions, etc.). You should be able to get around the problem by changing that line of code to this...
VBA Code:
V(i, 1) = VBA.Trim(V(i, 1))
It would be better to rename the module instead though.
 
Upvote 0
Solution
Do you have a module that you named "Trim"? If so, that is the problem (you should not use VBA keywords for the names of modules, variable, functions, etc.). You should be able to get around the problem by changing that line of code to this...
VBA Code:
V(i, 1) = VBA.Trim(V(i, 1))
It would be better to rename the module instead though.

Hi Rick,

Actually that was the issue, I thought my coworkers added the module with different name, thanks a lot.

Regards
Andres
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,858
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top