Delete empty space before and after cells - VBA

ToseSenpai

New Member
Joined
Apr 18, 2021
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Hi all,
there is a way to delete space before and after into cells with vba (Range B7-B50)?
I prefer VBA instead of formulas because i will insert the list of number in a columns and then press a button that "clear" my numbers from undesired space.


Thank you all for the help :)
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Assuming that these are normal spaces and not some special characters, here is one way to do it:
VBA Code:
Sub TrimCells()

    Dim cell As Range
   
    Application.ScreenUpdating = False
   
    For Each cell In Range("B7:B50")
        cell.Value = Trim(cell.Value)
    Next cell
   
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
Solution
Assuming that these are normal spaces and not some special characters, here is one way to do it:
VBA Code:
Sub TrimCells()

    Dim cell As Range
  
    Application.ScreenUpdating = False
  
    For Each cell In Range("B7:B50")
        cell.Value = Trim(cell.Value)
    Next cell
  
    Application.ScreenUpdating = True
  
End Sub
Grazie mille :)
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,203,472
Messages
6,055,610
Members
444,803
Latest member
retrorocket129

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