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
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
This assumes that the cells do not hold formulas and looks from B1 to last filled row in col B, adjust to suit:
VBA Code:
Sub RemoveLeadingAndTrailingSpaces()
Dim V As Variant, i As Long
V = Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row).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("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row).Value = V
        
End Sub
 
Upvote 0
This assumes that the cells do not hold formulas and looks from B1 to last filled row in col B, adjust to suit:
VBA Code:
Sub RemoveLeadingAndTrailingSpaces()
Dim V As Variant, i As Long
V = Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row).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("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row).Value = V
       
End Sub
Hi Joe, Its getting and error , can you please check?

1634520671076.png


Regards
 
Upvote 0
Hi Andresuru, Joe.
just stumbled across this thread looking for something else and was intrigued by the code solution. So Joe's code works perfectly for me (Thank you, Joe - this looks very useful!), so I can confirm that the problem isn't with the code. Looking at the screen capture that you helpfully provided, Andresuru, I suspect another problem might be with how you've opted to name your code module. It appears that you've called the active module "TRIM" - you really should avoid using VBA keywords, etc when naming modules and variables, etc - other wise VBA will get confused. Try changing the name of the module too and see how it goes.
 
Upvote 0
I agree, the module called 'Trim' would cause that problem. Rename that module and try again.
 
Upvote 0
Hi Andresuru, Joe.
just stumbled across this thread looking for something else and was intrigued by the code solution. So Joe's code works perfectly for me (Thank you, Joe - this looks very useful!), so I can confirm that the problem isn't with the code. Looking at the screen capture that you helpfully provided, Andresuru, I suspect another problem might be with how you've opted to name your code module. It appears that you've called the active module "TRIM" - you really should avoid using VBA keywords, etc when naming modules and variables, etc - other wise VBA will get confused. Try changing the name of the module too and see how it goes.

Hello Dan you are correct, I changed and that fix the issue.
 
Upvote 0
This assumes that the cells do not hold formulas and looks from B1 to last filled row in col B, adjust to suit:
VBA Code:
Sub RemoveLeadingAndTrailingSpaces()
Dim V As Variant, i As Long
V = Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row).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("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row).Value = V
        
End Sub

Thank you a lot for this vba Code, it works perfectly, I have a question I could not run through a button in another sheet do you know the reason ?
 
Upvote 0
Thank you a lot for this vba Code, it works perfectly, I have a question I could not run through a button in another sheet do you know the reason ?
Does it give error, improper results? What does "could not run through a button in another sheet" mean? Please provide more info.
 
Upvote 0
Thank you a lot for this vba Code, it works perfectly, I have a question I could not run through a button in another sheet do you know the reason ?
You are welcome - thanks for the reply.

Show the code you used so we can see if you correctly qualified the sheet you want to execute the code on.
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,731
Members
449,093
Latest member
Mnur

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