Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel - problem importing negative numbers from text file 4

Status
Not open for further replies.

PPettit

IS-IT--Management
Sep 13, 2003
511
US
When my accounting software generates a report, it writes all negative numbers with the dash (or "minus sign") after the number (ex: 123.45-).

Every now and then, I need to import these reports into Excel so that I can do some additional manipulation of the data. My problem is that Excel treats those negative numbers as text since they don't follow the usual format for negative numbers (ie. "-123.45" or "(123.45)"). I have to fix each negative number by hand if I want to have the totals calculated correctly.

Is there an easy way (in Excel) to convert these numbers from "###.##-" to "-###.##"?



 


Hi,

In a new column, assuming that your values are in column A...
[tt]
=if(right(A1,1)="-",left(A1,Len(A1)-1)*-1,A1)
[/tt]
Copy down thru data rows

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
Thanks, Skip. This should work just fine.
 
Sounds like this will be an ongoing issue. Try this macro which I wrote to resolve the same issue.
Code:
Sub MyNegNumbers()
'Converts any cell in current selection _
  with a number plus trailing "-"   into a _
  number that excel recognises _
  e.g.from 9999- to -9999
'does not affect cells where removing the _
 trailing zero does not leave a single numeric value _
 is therefore 'safe' to use.
Dim c As Range
Dim myvar As String

For Each c In Selection
If Len(c.Value) = 0 Then
Else

myvar = Val(Left(c.Value, Len(c.Value) - 1))
    If Right(c.Value, 1) = "-" Then
        If Len(myvar) = Len(c.Value) - 1 Then
        c.Value = myvar * -1
        End If
    End If
End If
Next c
End Sub


Gavin
 
Thanks, Gavin.

This seems to work well, also. It's always a good idea to have more than one way to accomplish the same task.
 
Gavin:
I used your Macro since this problem is a regular issue with me and it works great. I did notice that if the number ended in a zero (###.#0-)that it would not convert the number. Do you have any ideas?

Thanks.

David
 


Hi,

That is NOT a conversion issue -- its a DISPLAY issue.

Trailing zeros on DECIMAL, or leading zeros on INTEGER can be addressed using a DISPLAY FORMAT.

Skip,

[glasses] [red]Be Advised![/red] A chicken, who would drag a wagon across the road for 2 cents, is…
POULTRY in motion to PULLET for a PALTRY amount! [tongue]
 
Well spotted David. I don't think that I tested it at all with decimal numbers (didn't apply to my data). My intention for my original application was that one could select the entire used range and this would solve the issue without inadvertently changing cells ending in "-" that were not numeric.

I think that this fixes it, though I haven't tested extensively. Do let me know if you find any other bugs.

Code:
Sub MyNegNumbers()
'Converts any cell in current selection _
  with a number plus trailing "-"   into a _
  number that excel recognises _
  e.g.from 9999- to -9999
'does not affect cells where removing the _
 trailing zero does not leave a single numeric value _
 is therefore 'safe' to use.
Dim c As Range
Dim myvar As String

For Each c In Selection
If Len(c.Value) = 0 Then
Else

myvar = Left(c.Value, Len(c.Value) - 1)
    If Right(c.Value, 1) = "-" Then
        If Len(myvar) = Len(c.Value) - 1 Then
        On Error Resume Next
        c.Value = myvar * -1
        On Error GoTo 0
        End If
    End If
End If
Next c
End Sub

Best wishes


Gavin
 
For Info: In Excel 2003: Data,Text to columns, advanced allows one to specify trailing minus signs.


Gavin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top