Flip sign in VBA for hotkey - keep formatting, skip non-numeric values

CountingUpToN

New Member
Joined
Mar 10, 2019
Messages
4
I've been looking for some time for code to perform the simple operation of flipping numbers back and forth between positive and negative. As an accountant I've tried many, many versions. Some work quickly, but balk when it interacts with text - error #Value . Others are super slow as it tests first for numeric values and skips the ones that have text - timing out when it reaches tens of thousands of data points. Please help me. I know that the fast way is to put a -1 in a cell, copy, p-s-multiply but that's a lot of key strokes. I run the keyboard like no one else I've seen, so one quick bar hot key, and I am super happy.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Does this macro work for you...
Code:
Sub FlipSign()
  Dim Ar As Range
  For Each Ar In Selection.SpecialCells(xlConstants, xlNumbers)
    Ar.Value = -Ar.Value
  Next
End Sub
 
Upvote 0
Oh, no. :( I have to revise and extend my remarks about your greatness. So sad. The thing is the code is behaving just like the code I had for the If-Then statement. Since the test statement is now For Each, the code takes more than five minutes to flip 5,000 values. Back to the drawing board.
 
Upvote 0
Oh, no. :( I have to revise and extend my remarks about your greatness. So sad. The thing is the code is behaving just like the code I had for the If-Then statement. Since the test statement is now For Each, the code takes more than five minutes to flip 5,000 values. Back to the drawing board.
See if this modification makes it any faster...
Code:
Sub FlipSign()
  Dim Ar As Range
  For Each Ar In Selection.SpecialCells(xlConstants, xlNumbers).Areas
    Ar = Evaluate("-" & Ar.Address)
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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