My VBA is messing with date formats - how do i stop it?

DMO123

Board Regular
Joined
Aug 16, 2018
Messages
99
Hi All, i have the below VBA code which trims each cell but i have dates in the workbook and it seems to mess with the date format which then forces you to double click in each cell to correct. Not ideal when there is 10000 rows. Can someone help me on stopping from doing this OR update the code to trim column O only?

Thanks!

Code:
Sub CleanSheet1()    For Each Cell In ActiveSheet.UsedRange
        Cell.Value = Trim(Cell)
    Next Cell
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You could use the intersect of the used range and column O. Personally instead of the loop id use this:

Code:
Sub TrimEvaluate()

Dim x, lr As Long

lr = Range("O" & Rows.Count).End(xlUp).Row
Set x = Range("O1:O" & lr)
x = Evaluate("=IF(ROW(1:" & lr & "),TRIM(" & x.Address & "))")
Range("O1:O" & lr) = x

End Sub
 
Upvote 0
Try:-
Code:
Dim cell As Range
For Each cell In ActiveSheet.UsedRange
        If Not IsDate(cell.Value) Then
            cell.Value = Trim(cell.Value)
        End If
Next cell
 
Upvote 0
You could use the intersect of the used range and column O. Personally instead of the loop id use this:

Code:
Sub TrimEvaluate()

Dim x, lr As Long

lr = Range("O" & Rows.Count).End(xlUp).Row
Set x = Range("O1:O" & lr)
x = Evaluate("=IF(ROW(1:" & lr & "),TRIM(" & x.Address & "))")
Range("O1:O" & lr) = x

End Sub

Works perfectly thank you!
 
Upvote 0
Hi

Only text values can be trimmed.

If you have a big used range it is more efficient, quicker, if you only loop through the text values.

Remark: the first loop is for the constant text values and the second loop is for the text values resulting of formulas. In this case you lose the formulas.

Code:
Sub TrimAllText()
Dim r As Range

' trims all text constant values
For Each r In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants, xlTextValues)
            r.Value = Trim(r.Value)
Next r

' trims all text values result of formulas (you lose the formulas)
For Each r In ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas, xlTextValues)
            r.Value = Trim(r.Value)
Next r

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,447
Members
448,898
Latest member
drewmorgan128

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