kevin440red
New Member
- Joined
- May 23, 2011
- Messages
- 26
- Office Version
- 2019
I have the Macro that cleans numbers up all the basic but when it runs in a table it is very slow on a regular excel sheet it is fast.
how can I speed it up? I select the range to scrub or clean.
how can I speed it up? I select the range to scrub or clean.
VBA Code:
Sub BAZINGA_007()
'PURPOSE: Determine how many seconds it took for code to completely run
Dim StartTime As Double
Dim SecondsElapsed As Double
'Remember time when macro starts
StartTime = Timer
'*****************************
'Special Characters
For Each cel In Selection
For i = Len(cel.Value) To 1 Step -1
Select Case Mid(cel.Value, i, 1)
'This is an | sign
Case Chr(124)
cel.Value = Left(cel.Value, i - 1) & Right(cel.Value, Len(cel.Value) - i)
'This is an sign
Case Chr(127)
cel.Value = Left(cel.Value, i - 1) & Right(cel.Value, Len(cel.Value) - i)
'This is an Spaces
Case Chr(32)
cel.Value = Left(cel.Value, i - 1) & Right(cel.Value, Len(cel.Value) - i)
'This is an Spaces
Case Chr(160)
cel.Value = Left(cel.Value, i - 1) & Right(cel.Value, Len(cel.Value) - i)
'This is an # sign
Case Chr(35)
cel.Value = Left(cel.Value, i - 1) & Right(cel.Value, Len(cel.Value) - i)
'This is an _ sign
Case Chr(95)
cel.Value = Left(cel.Value, i - 1) & Right(cel.Value, Len(cel.Value) - i)
'This is an * sign
Case Chr(42)
cel.Value = Left(cel.Value, i - 1) & Right(cel.Value, Len(cel.Value) - i)
'This is an ` sign
Case Chr(39)
cel.Value = Left(cel.Value, i - 1) & Right(cel.Value, Len(cel.Value) - i)
'This is an - sign
Case Chr(45)
cel.Value = Left(cel.Value, i - 1) & Right(cel.Value, Len(cel.Value) - i)
End Select
Next i
Next cel
'Removes Carriage Returns
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim cell As range
'Also Treat CHR 0160, as a space (CHR 032)
Selection.Replace What:=Chr(160), Replacement:=Chr(32), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
'Do Same with carriage return (Alt-Enter)
'Also Treat CHR 010, as a space (CHR 032)
Selection.Replace What:=Chr(10), Replacement:=Chr(32), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
'Trim in Excel removes extra internal spaces, VBA does not
On Error Resume Next 'in case no text cells in selection
For Each cell In Intersect(Selection, _
Selection.SpecialCells(xlConstants, xlTextValues))
cell.Value = Application.Trim(cell.Value)
Next cell
On Error GoTo 0
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
'Makes 10 digit
Dim ThisCell As range
Application.ScreenUpdating = False
Selection.NumberFormat = "@"
For Each ThisCell In Selection
If Len(ThisCell) < 10 Then
ThisCell = Right("0000000000" & ThisCell, 10)
Else
End If
Next ThisCell
Application.ScreenUpdating = True
'Convert to General
Selection.NumberFormat = "General"
'Add Apostrophe to start of Part Number
Dim c As range
For Each c In Selection
If c.Value <> "" Then c.Value = "'" & c.Value
Next
'*****************************
'Determine how many seconds code took to run
SecondsElapsed = Round(Timer - StartTime, 2)
'Notify user in seconds
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
End Sub