Tuesday, April 25, 2006

Excel: Quick Macro to Set Selection Values to Negative

Here is a quick tip, lets say you have a Excel spreadsheet, and you quickly need to change a series of values to negative values. You can use the below macro, which demonstrates how to traverse through every cell in a selection and set the value to a negative value. This is useful for accounting formating.

Sub set_negative()
    Dim c As Range
    
    'For each cell in your selection
    For Each c In Selection
        'If the value is not blank, set to a negative version of
        'itself
        If (c.Value <> "") Then
            c.Value = c.Value * -1
        End If
    Next
End Sub

No comments: