Sub trimmer()
'
' trimmer Macro
'
' Keyboard Shortcut: Ctrl+t
'
On Error GoTo ErrorHandler
Dim rSel As Range
Set rSel = Selection
Dim c As Range
Dim vCalc As Variant
vCalc = Application.Calculation
Dim strV
Dim intConv As Integer
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
intConv = Application.InputBox("1. Date - 2. Currency - 3. Decimal - 4. long" & Chr(13) & _
"5. Don't convert just trim values" & Chr(13) & _
"6. Convert international (yyyymmdd) dates to normal dates" & Chr(13) & _
"7. Double", , , , , , , 1)
Set rSel = NonEmptyCells(rSel)
If rSel.Cells.Count > 5000 Then
If MsgBox("You have selected a large number of cells, this may take some time, do you want to continue?", vbOKCancel) = vbCancel Then
GoTo exiter
End If
End If
Select Case intConv
Case 1
For Each c In rSel
If c.Value <> "" Then
c.Value = CDate(Trim(c.Value))
c.NumberFormat = "dd-mmm-yyyy"
End If
Next c
Case 2
For Each c In rSel
If c.Value <> "" Then
c.Value = CCur(Trim(c.Value))
End If
Next c
Case 3
For Each c In rSel
If c.Value <> "" Then
c.Value = CDec(Trim(c.Value))
End If
Next c
Case 4
For Each c In rSel
If c.Value <> "" Then
c.Value = CLng(Trim(c.Value))
End If
Next c
Case 5
For Each c In rSel
If Trim(c.Value) = "" Then c.Value = ""
If c.Value <> "" Then
strV = Trim(c.Value)
While Asc(Left(strV, 1)) = 127 Or Asc(Left(strV, 1)) = 129 Or Asc(Left(strV, 1)) = 141 Or Asc(Left(strV, 1)) = 143 Or Asc(Left(strV, 1)) = 144 Or Asc(Left(strV, 1)) = 157 Or Asc(Left(strV, 1)) = 160 Or Asc(Left(strV, 1)) = 10 Or Asc(Left(strV, 1)) = 13
strV = Right(strV, Len(strV) - 1)
If Not strV <> "" Then GoTo skip
Wend
While Asc(Right(strV, 1)) = 127 Or Asc(Right(strV, 1)) = 129 Or Asc(Right(strV, 1)) = 141 Or Asc(Right(strV, 1)) = 143 Or Asc(Right(strV, 1)) = 144 Or Asc(Right(strV, 1)) = 157 Or Asc(Right(strV, 1)) = 160 Or Asc(Right(strV, 1)) = 10 Or Asc(Right(strV, 1)) = 13
strV = Left(strV, Len(strV) - 1)
If Not strV <> "" Then GoTo skip
Wend
skip:
c.Value = strV
End If
Next c
Case 6
'20110131'
For Each c In rSel
c.NumberFormat = "General"
If c.Value <> "" Then
c.Value = DateValue(Right(c.Value, 2) & "/" & Mid(c.Value, 5, 2) & "/" & Left(c.Value, 4))
End If
c.NumberFormat = "dd-mmm-yyyy"
Next c
Case 7
For Each c In rSel
If c.Value <> "" Then
c.Value = CDbl(Trim(c.Value))
End If
Next c
Case False
MsgBox ("you did not select a conversion type")
End Select
exiter:
Application.Calculation = vCalc
Application.ScreenUpdating = True
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
GoTo exiter
End Sub
Function NonEmptyCells(TestRange As Range) As Range
Dim r1 As Range
Dim r2 As Range
If Not TestRange.Cells.Count > 1 Then
Set NonEmptyCells = TestRange
Exit Function
End If
On Error Resume Next
Set r1 = TestRange.SpecialCells(xlCellTypeFormulas)
Set r2 = TestRange.SpecialCells(xlCellTypeConstants)
On Error GoTo 0
If r1 Is Nothing And Not r2 Is Nothing Then
Set NonEmptyCells = r2
ElseIf r2 Is Nothing And Not r1 Is Nothing Then
Set NonEmptyCells = r1
ElseIf r2 Is Nothing And r1 Is Nothing Then
Set NonEmptyCells = TestRange.Cells(1, 1)
Else
Set NonEmptyCells = Union(r1, r2)
End If
End Function