{"id":125,"date":"2008-08-19T12:53:20","date_gmt":"2008-08-19T11:53:20","guid":{"rendered":"http:\/\/simoncpage.co.uk\/blog\/?p=125"},"modified":"2008-10-19T14:54:08","modified_gmt":"2008-10-19T13:54:08","slug":"excel-vb-convert-positive-to-negative-and-negative-to-positive","status":"publish","type":"post","link":"https:\/\/simoncpage.co.uk\/blog\/2008\/08\/excel-vb-convert-positive-to-negative-and-negative-to-positive\/","title":{"rendered":"Excel VB | positive to negative &#038; vice versa"},"content":{"rendered":"<p>There are a\u00a0few ways of doing this.<\/p>\n<ol>\n<li>The simplest way to do this without any VB is as follow (it also will do negative to positive).\n<ol>\n<li>Add -1 to a cell on your sheet<\/li>\n<li>Copy that cell<\/li>\n<li>Select the range that you want to change the sign on<\/li>\n<li>Select &#8220;paste special&#8221; and from the operation section select multiple and click ok<\/li>\n<\/ol>\n<\/li>\n<li>You could multiple the range of numbers by another cell reference. This can be done manually or via VB. Here is some code that will do this for you (in a workbook download at end of this post).<\/li>\n<blockquote><p>Sub ApplyAFormulaToCell()<br \/>\nOn Error GoTo ENDER<\/p>\n<p>StringAction = Application.InputBox(&#8220;Type the formula to apply on the cells you have selected,&#8221; &amp; vbNewLine &amp; &#8220;i.e\u00a0 &#8216;\/1000&#8217;, &#8216;+4&#8217; or &#8216;*2.5&#8242;&#8221; &amp; vbNewLine &amp; &#8220;use the point as decimal separator. You can also use cell references (select with a mouse).&#8221;, &#8220;Apply formula&#8221;, &#8220;\/100&#8221;, , , , , 2)<\/p>\n<p>If StringAction = &#8220;&#8221; Then Exit Sub<br \/>\nIf StringAction = False Then Exit Sub<\/p>\n<p>Application.ScreenUpdating = False<br \/>\nApplication.Calculation = xlCalculationManual<br \/>\nCellLength = Selection.Cells.Count<br \/>\ni = 1<\/p>\n<p>For Each rngCel In Selection<\/p>\n<p>If rngCel.Formula &lt;&gt; &#8220;&#8221; Then<br \/>\nApplication.StatusBar = &#8220;Processing: &#8221; &amp; StringAction &amp; &#8221; &#8221; &amp; Int(i * 100 \/ CellLength) &amp; &#8220;%&#8221;<br \/>\nStringFormula = rngCel.FormulaR1C1<\/p>\n<p>If Left(StringFormula, 1) = &#8220;=&#8221; Then StringFormula = Right(StringFormula, Len(StringFormula) &#8211; 1)<br \/>\nIf rngCel.Value &lt;&gt; &#8220;&#8221; And IsNumeric(rngCel.Value) Then<br \/>\nrngCel.Formula = &#8220;=(&#8221; &amp; StringFormula &amp; &#8220;)&#8221; &amp; StringAction<br \/>\nEnd If<br \/>\nElse<br \/>\nEnd If<\/p>\n<p>i = i + 1<\/p>\n<p>Next rngCel<\/p>\n<p>Application.StatusBar = False<br \/>\nApplication.Calculation = xlCalculationAutomatic<br \/>\nExit Sub<\/p>\n<p>ENDER:<br \/>\nApplication.StatusBar = False<br \/>\nApplication.Calculation = xlCalculationAutomatic<br \/>\nApplication.ScreenUpdating = True<br \/>\nMsgBox &#8220;An error occured.&#8221; &amp; vbNewLine &amp; &#8220;Please check your formula: &#8216; &#8221; &amp; StringAction &amp; &#8221; &#8216;&#8221;, vbCritical<br \/>\nEnd Sub<\/p><\/blockquote>\n<li>VBA addin &#8211; this is for me not the simplest way to achieve this but it certainly the most convenient. The VBA is included in the example workbook at the bottom of this post. The added value you achieve with this is that you can add it to an addin and have it on a shortcut key.Plus I have three options with it 1 &amp; 2 are convert all to positive or negative. This is something that you can&#8217;t achieve with the above 2 solutions. 3rd option is invert positive to negative or negative to positive (as with the above selections). Here is the code for the form:<\/li>\n<blockquote><p>Option Explicit<br \/>\nDim RngCell\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 As Range<br \/>\nDim RngSelection\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 As Range<br \/>\nDim CellLen\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 As Long<br \/>\nDim i\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 As Long<\/p>\n<p>Private Sub cmdCancel_Click()<br \/>\nUnload Me<br \/>\nEnd Sub<\/p>\n<p>Private Sub cmdOK_Click()<br \/>\nApplication.ScreenUpdating = False<br \/>\nSet RngSelection = selection<br \/>\nselection.SpecialCells(xlCellTypeConstants, 1).Select<br \/>\nCellLen = selection.Cells.Count<br \/>\ni = 1<br \/>\nIf OptPositive = True Then Call ChangetoPositive<br \/>\nIf OptNegative = True Then Call ChangetoNegative<br \/>\nIf OptFlip = True Then Call Flip<br \/>\nApplication.StatusBar = False<br \/>\nApplication.ScreenUpdating = True<br \/>\nRngSelection.Select<br \/>\nMsgBox &#8220;Numbers converted&#8221;, vbInformation, &#8220;SP&#8221; &amp; &#8221; &#8211; Convert numbers&#8221;<br \/>\nEnd Sub<\/p>\n<p>Sub ChangetoPositive()<br \/>\nFor Each RngCell In selection<br \/>\nApplication.StatusBar = &#8220;Converting all numbers to positive :\u00a0\u00a0 &#8221; &amp; Int(i * 100 \/ CellLen) &amp; &#8220;%&#8221;<br \/>\nIf IsNumeric(RngCell.Value) Then<br \/>\nIf RngCell.Value &lt; 0 Then RngCell.Value = RngCell.Value * -1<br \/>\nEnd If<br \/>\ni = i + 1<br \/>\nNext<br \/>\nEnd Sub<\/p>\n<p>Sub ChangetoNegative()<br \/>\nFor Each RngCell In selection<br \/>\nApplication.StatusBar = &#8220;Converting all numbers to negative :\u00a0\u00a0 &#8221; &amp; Int(i * 100 \/ CellLen) &amp; &#8220;%&#8221;<br \/>\nIf IsNumeric(RngCell.Value) Then<br \/>\nIf RngCell.Value &gt; 0 Then RngCell.Value = RngCell.Value * -1<br \/>\nEnd If<br \/>\ni = i + 1<br \/>\nNext<br \/>\nEnd Sub<\/p>\n<p>Sub Flip()<br \/>\nFor Each RngCell In selection<br \/>\nApplication.StatusBar = &#8220;Converting all numbers to negative :\u00a0\u00a0 &#8221; &amp; Int(i * 100 \/ CellLen) &amp; &#8220;%&#8221;<br \/>\nIf IsNumeric(RngCell.Value) Then<br \/>\nRngCell.Value = RngCell.Value * -1<br \/>\nEnd If<br \/>\ni = i + 1<br \/>\nNext<br \/>\nEnd Sub<\/p><\/blockquote>\n<\/ol>\n<p>Here is the workbook with the above code in. Feel free to change it but if you use it please add my credit. Also if you have any other cool ways of doing this let me know. Thanks<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-98\" title=\"excel\" src=\"https:\/\/simoncpage.co.uk\/blog\/wp-content\/uploads\/2008\/08\/excel.gif\" alt=\"\" width=\"91\" height=\"92\" \/><\/p>\n<p><a href=\"https:\/\/simoncpage.co.uk\/blog\/wp-content\/uploads\/2008\/08\/converting-positive-negative.zip\">converting-positive-negative.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a\u00a0few ways of doing this. The simplest way to do this without any VB is as follow (it also will do negative to positive). Add -1 to a cell on your sheet Copy that cell Select the range that you want to change the sign on Select &#8220;paste special&#8221; and from the operation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[26],"tags":[123,79,78,117,27,119,121],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/posts\/125"}],"collection":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":0,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"wp:attachment":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}