Question : Excel Macro to Delete Leading Spaces

I have a small little VBA module that does a nice job of deleting the leading spaces in a cell.  However, this requires that I select a column or row before I run it.

I need to know how to select all the cells in the whole worksheet or even just select a Range of cells.

Any advice is appreciated.
thanks,
dale
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
Public Sub RemoveSpaces()
 
   Dim Cell As Range
      For Each Cell In Selection
      Cell = Trim(Cell)
   Next Cell
 
End Sub
Open in New Window Select All

Answer : Excel Macro to Delete Leading Spaces

Dale,

This  updates version prompts your for a range, the selection before the macro is the default selection

Cheers

Dave

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
Option Explicit
Option Base 1
Sub Clearer()
    Dim X(), myRange As Range, myArea As Range, i As Long, j As Long
    Set myRange = Application.InputBox("Pls select your range,", Default:=Selection.Address, Type:=8)
 
    Application.ScreenUpdating = False
 
    For Each myArea In myRange.Areas
        If myArea.Cells.Count > 1 Then
            X = myArea
            For i = 1 To myArea.Rows.Count
                For j = 1 To myArea.Columns.Count
                    X(i, j) = Trim(X(i, j))
                Next j
            Next i
            myArea = X
        Else
            myArea.Value = Trim(myArea)
        End If
        
    Next myArea
    Application.ScreenUpdating = True
End Sub
Open in New Window Select All
Random Solutions  
 
programming4us programming4us