Here's what I have so far:
Sub TrimColumnD()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim c As Range
For Each c In ActiveSheet.UsedRange.Columns("D").Cells
c.Value = WorksheetFunction.Trim(c.Value)
Next c
Next ws
End Sub
The trim function only works on the cells in the first worksheet.
Press F5 key to begin looping the column, then the cursor will stop at the first met blank cell.
One way to loop through a range is to use the For... Next loop with the Cells property. Using the Cells property, you can substitute the loop counter (or other variables or expressions) for the cell index numbers. In the following example, the variable counter is substituted for the row index.
Please change this line:
For Each c In ActiveSheet.UsedRange.Columns("D").Cells
into this one:
For Each c In ws.UsedRange.Columns("D").Cells
In your code internal loop refers to activesheet while it should refer to ws variable
representing sheet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With