Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Jan 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Recognizing a screen selection as range (VBA)

    I have an existing macro that changes the value of an activecell to negative. I now need to make this happen for a selected range of cells, but cannot figure out how to make the macro recognize the selection as a range and perform the formula on each cell. Any assistance would be greatly appreciated.

  2. #2
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Recognizing a screen selection as range (VBA)

    'Selection' -is- a range in Excel, so for example:

    For Each cell in Selection.Cells
    cell.Value = - cell.Value ' reverses the sign
    Next cell

    or

    For Each cell in Selection.Cells
    cell.Value = - Abs(cell.Value) ' make them all negative
    Next cell
    -John ... I float in liquid gardens
    UTC -7ąDS

  3. #3
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Recognizing a screen selection as range (VBA)

    Try the following:

    <pre>Public Sub MakeNegative()
    Dim oCell As Range
    For Each oCell In Selection
    If Not oCell.HasFormula And IsNumeric(oCell.Value) Then
    oCell.Value = -Abs(oCell.Value)
    End If
    Next oCell
    End Sub
    </pre>

    Legare Coleman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •