Thursday, April 14, 2011

Move Negative Sign

When importing data into Excel negative numbers may have the negative sign on the right side which will be viewed by Excel as text. The code below will move the sign to the left side.

Sub ConvertMirrorNegatives()
Dim rCell As Range
Dim rRange As Range
Dim lCount As Long
Dim lLoop As Long
If Selection.Cells.Count = 1 Then
MsgBox "Please select the range to convert", vbInformation
Exit Sub
End If

On Error Resume Next
Set rRange = Selection.SpecialCells(xlCellTypeConstants, xlTextValues)

If rRange Is Nothing Then
MsgBox "No mirror negatives found", vbInformation
On Error GoTo 0
Exit Sub
End If

lCount = WorksheetFunction.CountIf(Selection, "*-")
Set rCell = Selection.Cells(1, 1)

For lLoop = 1 To lCount
Set rCell = rRange.Find(What:="*-", After:=rCell, _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False)

rCell.Replace What:="-", Replacement:=""
rCell = rCell * -1
Next lLoop
On Error GoTo 0
End Sub

No comments:

Post a Comment