+ Reply to Thread
Results 1 to 5 of 5

Copy discontinuous range

Hybrid View

  1. #1
    Registered User
    Join Date
    10-13-2009
    Location
    Great Falls, Montana
    MS-Off Ver
    Excel 2007
    Posts
    32

    Copy discontinuous range

    I have a master list of items that I copy items from into individual sheets. Each item is a group of rows, and each group of rows is separated from the next group by a empty row that is formatted with bold borders. I only want to copy those columns with data so formulas in the target worksheet are not fouled up. Thus the copied range will be the intersection of the data columns and the rows between bold borders. I am trying to loop up from the current selection to a row with a bold border to find the top of the range, then loop down to find a row with a bold border to find the bottom of the range. But I'm having some trouble with the code. This is what I have so far:

    Sub CopyRng()
    ' Keyboard Shortcut: Ctrl+q
    Dim Rng1 As Range
    Dim Rng2 As Range
    Dim Rng3 As Range
    Dim Rng4 As Range
    Dim Rng5 As Range
    Dim rs As Range
    
    Set Rng1 = Range(Range("E7"), Range("E65536").End(xlUp))
    Set Rng2 = Range(Range("G7"), Range("J65536").End(xlUp))
    Set Rng3 = Range(Range("L7"), Range("O65536").End(xlUp))
    Set Rng4 = Range(Range("Q7"), Range("R65536").End(xlUp))
    
    Set Rng5 = Union(Rng1, Rng2, Rng3, Rng4)
    
    For r = (Selection.End(xlUp).Row) To r = 7 Step -1
        With Selection.Borders(xlEdgeTop)
            If .Weight = xlMedium Then
                t = Selection.Row
            End If
        End With
    Next r
            
    For r = 7 To r = (Selection.End(xlUp).Row) Step 1
        With Selection.Borders(xlEdgeBottom)
            If .Weight = xlMedium Then
                b = Selection.Row
            End If
        End With
    Next r
        
        rs = Range(Cells(t, 1), Cells(b, 18))
    
    Set CopyRng = Intersect(rs, Rng5)
        
        CopyRng.Select
        
    End Sub
    Any help is appreciated.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Copy discontinuous range

    Can you attach a small sample workbook? I find it hard to visualise things like this. You might be able to use CurrentRegion or Areas.

  3. #3
    Registered User
    Join Date
    10-13-2009
    Location
    Great Falls, Montana
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Copy discontinuous range

    Thank you for your help. Attached is a sample workbook. My real master list has several hundred such groups of rows. If I select any row within a group of rows, I would like to copy all the shaded columns (those that do not have formulas) within the group of rows and paste them to the end of another sheet. I'm getting hung up on finding the top and bottom of my groups of rows. Your help is appreciated.
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Copy discontinuous range

    To remove all doubt, please can you add to your file an illustration of your expected results?

  5. #5
    Registered User
    Join Date
    10-13-2009
    Location
    Great Falls, Montana
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Copy discontinuous range

    Thank you for your help. On the sample worksheet you can see three groups of rows, each separated by a merged cell with bold outline. The first two groups are populated with sample data, the third is not. This represents my master list, which will contain several hundred such groups, each with it's own sub-total line (labled "Direct Construction Cost"), a blank row, and a merged/bold row. The columns shaded grey here only contain data, the rest contain formulas.
    The individual sheets will already be populated with formulas that have various links. I don't want to mess those formulas up. That is why I only want to copy/paste the data columns that are shown on the sample as grey.
    When I select a cell in a group of rows, I want to select only the data columns from the merged/bold row above the selected cell down to and including the merged/bold row below the selected cell. I want to copy those 4 ranges and paste at the bottom of my individual sheet.
    I have two problems. The first is selecting the rows between merged/bold rows. The second is overcoming the limitation on copy/paste of discontinuous ranges.
    I hope that makes it more clear. Thanks again for your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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