+ Reply to Thread
Results 1 to 3 of 3

Copy noncontiguous range of cells to another sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Question Copy noncontiguous range of cells to another sheet

    I am using the following code to copy specific column cells to another sheet:
    Dim LRow As Long, ChkBx As CheckBox, WS2 As Worksheet
        Set WS2 = Worksheets("Sheet2")
        LRow = WS2.Range("A" & Rows.Count).End(xlUp).Row
        For Each ChkBx In ActiveSheet.CheckBoxes
            If ChkBx.Value = 1 Then
            LRow = LRow + 1
            Range("B" & ChkBx.TopLeftCell.Row, "D" & ChkBx.TopLeftCell.Row).Select
            Selection.Copy
            WS2.Select
            WS2.Range("A" & LRow).Select
            ActiveSheet.Paste
            End If
        Next
        Sheets("Sheet2").Activate
    I am trying to use the below code to select the specific cells, but it selects the entire range from B to D that also includes C column, whereas i need only B and D column. Is there any way to achieve this? Please suggest the changes (also i am looking for a general solution where where we can achieve this for any number of noncontiguous cells):
    Range("B" & ChkBx.TopLeftCell.Row, "D" & ChkBx.TopLeftCell.Row).Select

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Copy noncontiguous range of cells to another sheet

    Try:

    Union(Range("B" & ChkBx.TopLeftCell.Row), Range("D" & ChkBx.TopLeftCell.Row)).Copy Destination:=WS2.Range("A" & LRow)

  3. #3
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Copy noncontiguous range of cells to another sheet

    @Andrew-R: Whoa! That was quick!! Thanks a ton bro! Finally its working!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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