+ Reply to Thread
Results 1 to 7 of 7

Invert Selection of Cells

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-30-2008
    Location
    Eau Claire, WI
    Posts
    184

    Invert Selection of Cells

    I'm trying to highlight a bunch of cells in clusters not close together, and then invert the selection so that all cells BUT these are selected (therefore capturing all possible cells that someone could scroll to). In XL03 you could go to File > Invert Selection and it would work fine. But in XL07 which I have now, there doesn't seem to be a way to do it. Also, when using ctrl^a to highlight everything, and while still holding control in XL03 you could de-select specific cells, not so much in 07. I need to know if there's a way to do it with no macros involved. If not, I need a macro to do such a function on any given worksheet. Thanks!
    Last edited by jman0707; 10-10-2008 at 01:33 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    I've never heard of a standard feature to invert selection in Excel 2003 or any other version. It's easy enough (but not particularly efficient) to do in VBA, given some particulars.

    Invert the selection with respect to what -- all the cells on the worksheet, the used range, the bounding box of the current selection, or ...?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    09-30-2008
    Location
    Eau Claire, WI
    Posts
    184
    I would want to invert with respect to all the cells on the worksheet (seen and unseen). At least that's what my application needs now. But I can see use of inverting it with respect to the bounding box of the current cells (only the ones seen). Maybe you could help me with both? I know nothing about VBA, so detailed instructions would be very much appreciated! And I'd like to create buttons to execute the macro if possible. Thanks!

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Here are some range set algebra functions you can play with.
    ' ======================================
    ' Set Algebra Functions for Ranges
    ' shg 2007-0720
    ' Rev 2008-1010
    ' ======================================
    
    Function RangeXOr(rngA As Range, rngB As Range) As Range
        ' Returns Symmetric Difference of rngA and RngB
        ' RangeXOR = Union(rngA, rngB) - Intersect(rngA, RngB)
    
        Dim cell    As Range
        
        If rngA Is Nothing Then
            Set RangeXOr = rngB
        
        ElseIf rngB Is Nothing Then
            Set RangeXOr = rngA
        
        Else
            For Each cell In Union(rngA, rngB)
                If Intersect(cell, rngA, rngB) Is Nothing Then
                    If RangeXOr Is Nothing Then
                        Set RangeXOr = cell
                    Else
                        Set RangeXOr = Union(RangeXOr, cell)
                    End If
                End If
            Next
        End If
    End Function
    
    Function RangeComp(rngA As Range, rngB As Range) As Range
        ' Returns the Relative Complement of rngA in rngB
        ' RangeComp = rngB - RngA
    
        Dim cell    As Range
    
        If rngA Is Nothing Then
            Set RangeComp = rngB
        
        ElseIf rngB Is Nothing Then
            ' nothing to do; will return Nothing
        Else
            For Each cell In rngB
                If Intersect(cell, rngA) Is Nothing Then
                    If RangeComp Is Nothing Then
                        Set RangeComp = cell
                    Else
                        Set RangeComp = Union(RangeComp, cell)
                    End If
                End If
            Next
        End If
    End Function
    
    Function RangeBox(rng As Range) As Range
        ' Returns a single range bounding the cells in rng
        Dim iRowBeg As Long, iRowEnd As Long
        Dim iColBeg As Long, iColEnd As Long
        Dim iRow    As Long, iCol    As Long
        Dim nRow    As Long, nCol    As Long
        
        Dim rArea   As Range
        Dim wksDad  As Worksheet
        
        Set wksDad = rng.Parent
        iRowBeg = wksDad.Columns.Count
        iColBeg = wksDad.Rows.Count
        
        If rng.Areas.Count = 1 Then
            Set RangeBox = rng
        Else
            For Each rArea In rng.Areas
                iRow = rArea.Row
                iCol = rArea.Column
                nRow = rArea.Rows.Count
                nCol = rArea.Columns.Count
                iRowBeg = IIf(iRow < iRowBeg, iRow, iRowBeg)
                iColBeg = IIf(iCol < iColBeg, iCol, iColBeg)
                iRowEnd = IIf(iRow + nRow - 1 > iRowEnd, iRow + nRow - 1, iRowEnd)
                iColEnd = IIf(iCol + nCol - 1 > iColEnd, iCol + nCol - 1, iColEnd)
            Next rArea
            With wksDad
                Set RangeBox = Range(.Cells(iRowBeg, iColBeg), .Cells(iRowEnd, iColEnd))
            End With
        End If
    End Function
    
    Function IsSameRange(r1 As Range, r2 As Range) As Boolean
        Dim rArea As Range
        
        For Each rArea In Union(r1, r2).Areas
            If Intersect(rArea, r1).Address <> rArea.Address Then Exit Function '-->
            If Intersect(rArea, r2).Address <> rArea.Address Then Exit Function '-->
        Next
        IsSameRange = True
    End Function
    Select some odd collection of cells, and from the VBE Immediate window, try
    RangeBox(selection).Select
    RangeComp(selection, RangeBox(selection)).Select
    Last edited by shg; 10-10-2008 at 04:41 PM. Reason: Correction to RangeBox function

  5. #5
    Forum Contributor
    Join Date
    09-30-2008
    Location
    Eau Claire, WI
    Posts
    184
    Awesome thanks!

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Stop back and pick up a fresh copy -- there was an error in the RangeBox function.

  7. #7
    Forum Contributor
    Join Date
    09-30-2008
    Location
    Eau Claire, WI
    Posts
    184
    Awesome, did you just edit your post above? I'll grab it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro to address cells in the next column to selection
    By miles_muso in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-16-2008, 07:34 AM
  2. Macro to Lock cells in protected sheet depending on criteria
    By Zyphon in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-30-2008, 12:00 PM
  3. Replies: 0
    Last Post: 07-17-2007, 03:23 PM
  4. Block cells for selection
    By Pyrex238 in forum Excel General
    Replies: 1
    Last Post: 07-11-2007, 07:23 PM
  5. Problem Linking Cells in Different Workbooks
    By jeffc4442 in forum Excel General
    Replies: 7
    Last Post: 02-23-2007, 11:01 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1