I have a macro that I use to clean my used range, but it's slow, and I notice my mouse is flickering while it runs. I'd appreciate any help I can get in trouble-shooting it:Sub Cells_CleanUsedRange() ' "Cleans" contents of the used range on the active worksheet With Application .ScreenUpdating = False With ActiveSheet Dim Cell As Range .UsedRange.Select For Each Cell In Selection If Not Cell.HasFormula Then Cell.Value = CleanString(Cell.Value) End If Next Cell .UsedRange.Columns.AutoFit .UsedRange.Rows.AutoFit End With End With End Sub
Last edited by jomili; 12-30-2011 at 11:53 AM.
What does your CleanString function look like?
Here is how I would do the above code:
Sub Cells_CleanUsedRange() ' "Cleans" contents of the used range on the active worksheet Dim cll As Range With Application .ScreenUpdating = False With ActiveSheet For Each cll In .UsedRange If Not cll.HasFormula Then cll.Value = CleanString(cll.Value) End If Next cll .UsedRange.Columns.AutoFit .UsedRange.Rows.AutoFit End With .ScreenUpdating = True End With End Sub
hi jomili, two questions:
1. Why do you use Select in your code?
2. The flickering comes from looping the cells I guess. You could possibly try to use Specialcells (data volume is unknown)
You could also try this:
Sub Cells_CleanUsedRange() ' "Cleans" contents of the used range on the active worksheet Dim UsdRngValues As Variant Dim UsdRngFormulas As Variant Dim x As Long Dim y As Long With Application .ScreenUpdating = False With ActiveSheet UsdRngValues = .UsedRange.Value UsdRngFormulas = .UsedRange.Formula For x = 1 To UBound(UsdRngValues, 2) For y = 1 To UBound(UsdRngValues, 1) If UsdRngValues(y, x) = UsdRngFormulas(y, x) Then .Cells(y, x) = CleanString(UsdRngValues(y, x)) End If Next y Next x .UsedRange.Columns.AutoFit .UsedRange.Rows.AutoFit End With .ScreenUpdating = True End With End Sub
Or
Option Explicit Sub CleanUsedRange() ' "Cleans" contents of the used range on the active worksheet On Error GoTo exit_proc With ActiveSheet.UsedRange .SpecialCells(xlCellTypeConstants).ClearContents .Columns.AutoFit .Rows.AutoFit End With exit_proc: MsgBox "No cells to clean" End Sub
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
Roy,
I was working on the same lines as you, prompted by Watersev's suggestion. Here's my crude attempt:Yours looks a lot cleaner. Why did you use xlCellTypeConstants? I would have thought Visible would work...never mind, it just clicked! Using constants I don't have to have my check for HasFormula. Duh! You make it look so easy!Dim Cell As Range With ActiveSheet For Each Cell In .UsedRange.SpecialCells(xlCellTypeVisible) If Not Cell.HasFormula Then Cell.Value = CleanString(Cell.Value) End If Next Cell .UsedRange.Columns.AutoFit .UsedRange.Rows.AutoFit End With End Sub
Thanks to everyone for all the help on this one. I think you've all improved my coding loads. I appreciate all your efforts.
Thanks, and Happy New Year,
John
Roy,
I just noticed your code uses "ClearContents", not "CleanString". How do we adapt it? I triedbut that resulted in an error..SpecialCells(xlCellTypeConstants).value = CleanString(SpecialCells(xlCellTypeConstants).Value)
Try:
Sub CleanUsedRange() ' "Cleans" contents of the used range on the active worksheet Dim Cll As Range On Error GoTo exit_proc With ActiveSheet.UsedRange For Each Cll In .SpecialCells(xlCellTypeConstants) Cll.Value = CleanString(Cll.Value) Next Cll .Columns.AutoFit .Rows.AutoFit End With exit_proc: MsgBox "No cells to clean" End Sub
Thanks Whizbang; that does the trick! Thanks so much for the followthrough.
Happy New Year!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks