VBA for removing spaces in column D before first real character for multiple rows

BudZ 121 Reputation points
2021-02-13T17:12:17.267+00:00

Hello,

I have the following code we are using when we select column D for how many rows there are.

It is the same column field all the time but the amount of rows vary.

Would like the macro to just run without having to highlight ColumnD from row 2 through whatever just to remove the 2 or 3 leading spaces of the description.

Can someone help with this?

Thanks

Sub Removespaces()
'
' Removespaces Macro
'

Dim Rng As Range
Set Rng = Selection
For Each Cell In Rng
Cell.Value = Trim(Cell)
Next Cell

'Remove leading and trailing spaces in cell (e3)

'
End Sub

Developer technologies | Visual Basic for Applications
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-02-13T18:43:41.97+00:00

    Try something like this:

    Sub TrimLeadingSpacesColumnD()
    
        Dim ws As Worksheet
        Set ws = ActiveSheet
    
        Dim r As Range
        Set r = Application.Intersect(ws.Range("D2:D" & ws.Rows.Count), ws.UsedRange)
    
        Dim c As Range
        For Each c In r
            c.Value = LTrim(c.Value)
        Next
    
        r.Select
    
    End Sub
    

    You can use the name of worksheet or Sheets(name) instead of ActiveSheet.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.