0

Since I know, how to manage tih the TRIM function ddon to the last row, as advised here:

How to remove spaces from an entire Excel column using VBA?

or here:

Trim Cells using VBA in Excel

I want to do the same down to the last column.

In this case I prepared the following code:

  Sub trimit()
  Dim wks As Worksheet
  Dim lRow As Long, i As Long, lCol As Long

 Set wks = ThisWorkbook.ActiveSheet
  With wks
     lRow = .Columns("B:K").End(xlDown).Row
     lCol = Cells(1, Columns.Count).End(xlToLeft).Column
     For i = 1 To lRow
         .Cells(i, lCol).Value = trim(.Cells(i, lCol).Value)
     Next i
 End With
  End Sub

There is no error, but the code results fromenter image description here no reaction, or at most changes in column B only.

How can I apply the same for all the columns down to the last one?

3
  • 2
    Why don't you trim a whole range at once instead of a loop? Commented Feb 5, 2021 at 14:10
  • I don't know how to do it Commented Feb 5, 2021 at 14:14
  • 2
    The answer is literally there in the link i enclosed. Commented Feb 5, 2021 at 14:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.