Private Sub CommandButton1_Click()
On Error GoTo theEnd
Dim cell As Range
For Each cell In Selection.Cells
' per cell
If Left(cell.Formula, 1) <> "=" Then
' not a formula, simple multiplier
cell.Formula = "=-1*" & cell.Formula
Else
' formula, strip `=` and add parens
cell.Formula = "=(" & Right(cell.Formula, Len(cell.Formula) - 1) & ")*-1"
End If
Next cell
theEnd:
End Sub>
Sub FlipSign()
For Each Cell In Selection
If Left(Cell.FormulaR1C1, 1) = "=" Then
Cell.FormulaR1C1 = Cell.FormulaR1C1 & "*-1"
Else
Cell.FormulaR1C1 = "=" & Cell.FormulaR1C1 & "*-1"
End If
Next Cell
End Sub
Note it gets a little wonky if you run it multiple times on the same cells, since it just keeps adding "*-1" to the formula.
Sub Flip()
Dim c as range
If TypeName(Selection) <> "Range" Then Exit Sub
on error resume next
For Each c In Selection.cells
If isnumeric(c.value) Then
c.value = c.value * -1
End if
Next c
EndMacro:
End Sub>
Private Sub CommandButton1_Click()
On Error GoTo theEnd
' If TypeName(Selection) <> "Range" Then Exit Sub
Dim cell As Range
For Each cell In Selection.Cells
' per cell
If IsNumeric(cell.Value) Then
If Left(cell.Formula, 1) <> "=" Then
' not a formula, simple multiplier
cell.Formula = "=" & cell.Formula & "*-1"
Else
' formula
If Right(cell.Formula, 4) = ")*-1" Then
' it's already been changed so remove the parens and `*-1`
cell.Formula = "=" & Mid(cell.Formula, 3, Len(cell.Formula) - 6)
ElseIf Right(cell.Formula, 3) = "*-1" Then
' was originally a static number
cell.Value = Mid(cell.Formula, 2, Len(cell.Formula) - 4)
Else
' default value, strip `=` and add parens
cell.Formula = "=(" & Right(cell.Formula, Len(cell.Formula) - 1) & ")*-1"
End If ' already been changed test
End If ' formula test
End If ' numeric test
Next cell
theEnd:
End Sub
>>
posted by notsnot at 6:35 AM on January 7, 2009