You are here:

Excel/Switching negative sign

Advertisement


Question
-------------------------
Followup To
Question -
-------------------------
Followup To
Question -
-------------------------
Followup To
Question -
Hi Bill,

Our General Ledger software displays the negative sign at the end of the number and Excel does not recognize this as a number. How would I move the negative sign to the front of the number?

Thanks
Answer -
Go to the cell where the number is you want to change and edit the cell.  Delete the negative sign at the end of the cell's contents, type a negative sign at the beginning of the cell contents, and then press enter.


Hi Bill,

That part I knew.  I should have been more specific. Is there a macro that would automate this? It would make General Ledger analysis a lot more easier.

Thanks
Answer -
There is no macro that exists that will do this or practically any other task - you have to write them yourself or get someone to write them for you.  Here is a formula that will do what you want.  It tests A1

=IF(RIGHT(TRIM(A1),1)="-",LEFT(TRIM(A1),LEN(TRIM(A1))-1)*-1,A1)

A macro will need to loop thru ALL cells in the sheet where the data is and do the conversion.  Since I do not know where the data is and/or how large of an area it covers (you did not say) then here is a generic macro that covers only A1:B10


Dim X As Range
Dim Y As Range

Set Y = Range("A1:B10")

For Each X In Y
If X <> "" Then
If Right(Trim(X), 1) = "-" Then
X = Left(Trim(X), Len(Trim(X)) - 1) * -1
End If
End If
Next X


Hi Bill,

When I execute the above macro, the VBE Editor displays an error - "Invalid Outside Procedure".  What would be the fix?

Thanks
Answer -
There is nothing wrong the code.  What did you name the macro you put the code in I sent and where did you put the macro?

I did not name it anything. I just copied it into a blank module in VBE. When I executed it, it displayed the error.

Answer
<sigh>  I assumed you knew something about macros.  Again, you did not say.  All I have to go is what you say - nothing more, nothing less.

Copy this to the VBE

Sub FixNegativeSigns()
Dim X As Range
Dim Y As Range

Set Y = Range("A1:B10")

For Each X In Y
If X <> "" Then
If Right(Trim(X), 1) = "-" Then
X = Left(Trim(X), Len(Trim(X)) - 1) * -1
End If
End If
Next X
End Sub
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill

Expertise

I can provide help with most all EXCEL questions and most all questions about writing EXCEL macros. I have been developing macros for about 10 years in EXCEL and have switched to it from Lotus 1-2-3 after about 10 years of writing macros in it. Typically, I will not write a macro for you unless it is very short because of all the details a macro has to know about to work every time all the time. Please understand that I do not know it all and will be the first to say so. As politely as possible, I don't write macros for people on this site who need one, want one, seem to imply that they need one, and/or seem to think I am expected to write one UNLESS they are very short, quick, and simple. 99% of all macros are more involved than what you think and rarely am I provided with enough specific and complete details to have the code work the first time and every time. This typically means too many follow-up emails, and subsequent macro changes due to lack of specific details, just to get those details so that the macro would work, all of which is on my own free time. The voice of experience from responding to many questions from people who ask me to write a macro for them from this site tells me this. I don't mean to come across as unhelpful but macros are usually very specific and without ALL of the specifics the macro I would write will not address all of your needs and the layout, location, formatting, conditions, etc. of your data and any related files the macro would have to work with. What seems like a simple task to you is almost always more involved than what you think to have the macro ALWAYS work in EVERY situation. If you have a macro you have already written and have a question about it then perhaps I could help with that. I am sure and hope you can and do understand.

©2012 About.com, a part of The New York Times Company. All rights reserved.