+ Reply to Thread
Results 1 to 9 of 9

Copy range downwards till certain cell

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-19-2009
    Location
    Netherlands, Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    585

    Copy range downwards till certain cell

    Hello all,

    I'm looking for a way to copy a dynamic range several times downwards till a certain cell.
    i.e
    My dynamic range starts at B3:B..
    Ouput starts at H3:H100

    Any ideas?
    Attached Files Attached Files
    Last edited by Jonathan78; 12-25-2013 at 12:45 PM.

  2. #2
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Copy range downwards till certain cell

    Hi Jonathan78,

    There may be more elegant ways to do this, but this one should do the job.

    Try this:

    Sub main()
    
        Dim rngA As Range, rngB As Range
        Dim lRow As Long, lRowEnd As Long
    
        lRowEnd = 100        'change end row here
    
        With Sheets(1)
    
            Set rngA = .Range("B3", .Range("B" & Rows.Count).End(xlUp))
            Set rngB = .Range("D3", .Range("D" & Rows.Count).End(xlUp))
    
            lRow = rngA.Cells(1).Row
            Do
                lRow = lRow + rngA.Rows.Count
                .Cells(lRow, rngA.Column).Resize(rngA.Rows.Count, 1) = rngA.Value
            Loop While lRow < lRowEnd
    
            lRow = rngB.Cells(1).Row
            Do
                lRow = lRow + rngB.Rows.Count
                .Cells(lRow, rngB.Column).Resize(rngB.Rows.Count, 1) = rngB.Value
            Loop While lRow <= lRowEnd
    
            Application.Intersect(.Columns(rngA.Column), .Rows((lRowEnd + 1) & ":" & Rows.Count)).ClearContents
            Application.Intersect(.Columns(rngB.Column), .Rows((lRowEnd + 1) & ":" & Rows.Count)).ClearContents
    
        End With
        
        Set rngA = Nothing
        Set rngB = Nothing
    
    End Sub
    Last edited by berlan; 12-23-2013 at 05:05 PM.

  3. #3
    Forum Contributor
    Join Date
    08-19-2009
    Location
    Netherlands, Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    585

    Re: Copy range downwards till certain cell

    Thanks for helping Berlan,

    Is it possible to get the result in column H instead of underneath the existing range?

  4. #4
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Copy range downwards till certain cell

    In that case, try this:

    Sub main()
    
        Dim rngA As Range, rngB As Range
        Dim lRow As Long, lRowEnd As Long
    
        lRowEnd = 100        'change end row here
    
        With Sheets(1)
    
            Set rngA = .Range("B3", .Range("B" & Rows.Count).End(xlUp))
            Set rngB = .Range("D3", .Range("D" & Rows.Count).End(xlUp))
    
            lRow = rngA.Cells(1).Row
            Do
                .Cells(lRow, "H").Resize(rngA.Rows.Count, 1) = rngA.Value
                lRow = lRow + rngA.Rows.Count
            Loop While lRow <= lRowEnd
    
            lRow = rngB.Cells(1).Row
            Do
                .Cells(lRow, "J").Resize(rngB.Rows.Count, 1) = rngB.Value
                lRow = lRow + rngB.Rows.Count
            Loop While lRow <= lRowEnd
    
            Application.Intersect(.Columns("H"), .Rows((lRowEnd + 1) & ":" & Rows.Count)).ClearContents
            Application.Intersect(.Columns("J"), .Rows((lRowEnd + 1) & ":" & Rows.Count)).ClearContents
    
        End With
        
        Set rngA = Nothing
        Set rngB = Nothing
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    08-19-2009
    Location
    Netherlands, Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    585

    Re: Copy range downwards till certain cell

    That works fine Berlan.

    is it possible to start from a selected cell?

        Dim rngA As Range, rngB As Range
        Dim lRow As Long, lRowEnd As Long
    
        lRowEnd = 100        'change end row here
    
        With Worksheets("RS")
            Set rngA = .Range("AF7", .Range("AF" & Rows.Count).End(xlUp)).Offset(, 1)
    
            lRow = rngA.Cells(1).Row
            Do
                Worksheets("A").Cells(lRow, "O").Resize(rngA.Rows.Count, 1) = rngA.Value
                lRow = lRow + rngA.Rows.Count
            Loop While lRow <= lRowEnd
            
            Application.Intersect(.Columns("O"), .Rows((lRowEnd + 1) & ":" & Rows.Count)).ClearContents
        End With
    
        Set rngA = Nothing

  6. #6
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Copy range downwards till certain cell

    In this example, you can select both your input and output range. Try this:

    Sub main()
    
        Dim rngInput As Range, rngOutput As Range
        Dim lRow As Long, lRowEnd As Long
    
    CalculateInput:
        Set rngInput = Application.InputBox("Select input cells", Type:=8)
        If rngInput.Columns.Count > 1 Then MsgBox "Only select cells from one column": GoTo CalculateInput
        
    CalculateOutput:
        Set rngOutput = Application.InputBox("Select output cells", Type:=8)
        If rngOutput.Columns.Count > 1 Then MsgBox "Only select cells from one column": GoTo CalculateOutput
        
        With ThisWorkbook.Sheets(rngOutput.Parent.Name)
            lRow = rngOutput.Cells(1).Row
            lRowEnd = rngOutput.Rows.Count + lRow - 1
            Do
                .Cells(lRow, rngOutput.Cells(1).Column).Resize(rngInput.Rows.Count, 1) = rngInput.Value
                lRow = lRow + rngInput.Rows.Count
            Loop While lRow <= lRowEnd
            Application.Intersect(.Columns(rngOutput.Cells(1).Column), .Rows((lRowEnd + 1) & ":" & Rows.Count)).ClearContents
            .Activate
        End With
        
        Set rngInput = Nothing: Set rngOutput = Nothing
        
    End Sub
    Happy holidays!

  7. #7
    Forum Contributor
    Join Date
    08-19-2009
    Location
    Netherlands, Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    585

    Re: Copy range downwards till certain cell

    This is just awesome and will definitely use it in the future!
    But for now I'd like the user only to select the starting cell from where on the range will be copied down till the 100th row.

  8. #8
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Copy range downwards till certain cell

    Applying it to your latest code, which I am then unable to test properly without seeing more of the workbook, so try if this would work?

    You will select the starting cell from where the range will be copied down to to the 100th row.

    Sub main()
    
        Dim rngA As Range, rngB As Range
        Dim lRow As Long, lRowEnd As Long
        
    CalculateOutput:
        Set rngB = Application.InputBox("Select output cells", Type:=8)
        If rngB.Cells.Count > 1 Then MsgBox "Please select a single cell": GoTo CalculateOutput
        
        With Sheets("RS")
            Set rngA = .Range("AF7", .Range("AF" & Rows.Count).End(xlUp)).Offset(, 1)
        End With
    
        With ThisWorkbook.Sheets(rngB.Parent.Name)
            lRow = rngB.Cells(1).Row
            lRowEnd = 100   'copied down to the 100th row
            Do
                .Cells(lRow, rngB.Cells(1).Column).Resize(rngA.Rows.Count, 1) = rngA.Value
                lRow = lRow + rngA.Rows.Count
            Loop While lRow <= lRowEnd
            Application.Intersect(.Columns(rngB.Cells(1).Column), .Rows((lRowEnd + 1) & ":" & Rows.Count)).ClearContents
            .Activate
        End With
        
        Set rngA = Nothing: Set rngB = Nothing
        
    End Sub

  9. #9
    Forum Contributor
    Join Date
    08-19-2009
    Location
    Netherlands, Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    585

    Re: Copy range downwards till certain cell

    That's it!
    Thank you so much for helping me out with this.
    Happy Holidays!

+ 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. [SOLVED] Search backward in range till empty cell
    By fedude in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 08-06-2013, 08:00 AM
  2. [SOLVED] VBA copy down till the specific coulmn range
    By vaibhav2312 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-28-2013, 01:06 PM
  3. Replies: 3
    Last Post: 11-20-2012, 02:25 PM
  4. copy cell and paste diwn till last row
    By dingdang in forum Excel General
    Replies: 1
    Last Post: 09-24-2012, 01:22 PM
  5. copy till last used range
    By farrukh in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-02-2012, 10:24 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