Macro to move "-"

stapuff

Well-known Member
Joined
Feb 19, 2004
Messages
1,126
I am not sure of the author of this code - so I can't give proper credit. It is not doing exactly as I need so I need some help.

I have data downloaded where negative numbers are shown as 35,650- which is viewed by Excel as text. The code below moves the "-" from the right to the left thus allowing Excel to understand it as a negative which is what I need.

The problem is it get's trimmed. 35,650- shows as -35 not -35,650. What needs to change to show the whole #?

Thanks,

Kurt


Private Sub texttonegative()
Dim MemberCell As Range, DACELL
For Each MemberCell In ActiveSheet.UsedRange
If Right(MemberCell.Formula, 1) = "-" Then
MemberCell.Formula = Trim(MemberCell.Formula)
For DACELL = 1 To Len(MemberCell.Formula)
If Mid(MemberCell.Formula, DACELL, 1) = Chr(44) Then 'IF HAS COMMA
MemberCell.Formula = Left(MemberCell.Formula, DACELL - 1) _
& Right(MemberCell.Formula, Len(MemberCell.Formula) - 2)
End If
Next
MemberCell.Formula = _
Val("-" & Val(Left(MemberCell.Formula, _
Len(MemberCell.Formula) - 1))) 'Formula for moving sign
End If
Next
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I got a nice lil module for ya... Used it many times

Code:
Private Sub MoveNegSymbol()
'-------------------------------------->
'MOVES NEGATIVE SYMBOL FROM RIGHT TO LEFT
'-------------------------------------->
'change H2 and K65536 to meet your range requirements
For Each Cell In Range("H2", Range("K65536").End(xlUp).Address)
    If Right(Cell, 1) = "-" Then
        Cell.Value = --("-" & Left(Cell, Len(Cell) - 1))
        Cell.Font.ColorIndex = 3
    End If
Next Cell
End Sub

this also makes the text red :)
 
Upvote 0
Here's a macro that I use. But Mark W. is also correct and no macro is really needed. This is a macro I had before I knew that trick

Code:
Sub MoveMinus()
On Error Resume Next
Dim Cel As Range
Dim myVar As Range
Set myVar = Selection

For Each Cel In myVar
    If Right((Trim(Cel)), 1) = "-" Then
        Cel.Value = Cel.Value * 1
    End If
Next
 
   With myVar
      .Columns.AutoFit
End With

End Sub

HTH
texasalynn
 
Upvote 0
Indeed ;) But since you know the number is going to be negative theres no reason to have a selectable case as that provides... Either way works just as well tho
 
Upvote 0
Which version of Excel does the Text to Columns.., method work?

I'm using 2000 and can seem to find anything to do with trailing negatives.
 
Upvote 0
Norie said:
Which version of Excel does the Text to Columns.., method work?

I'm using 2000 and can seem to find anything to do with trailing negatives.

I'm using 2002, but I thought it was supported in 2000. See the [ Advanced... ] button at Step 3 of 3.
 
Upvote 0
scrupul0us said:
Indeed But since you know the number is going to be negative theres no reason to have a selectable case as that provides... Either way works just as well tho

Until someone edits the value and makes it positive.
 
Upvote 0
Checked the advanced button, it only gave me options to do with the Decimal and Thousand seperator.
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,178
Members
448,871
Latest member
hengshankouniuniu

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