简体   繁体   中英

Excel vba copy and paste loop within loop - limit range

Newbee here to both this site and Excel VBA. I used RichA's code in the below post and was able to make it work well for my purpose of populating/copying data in on sheet (Sheet2) from another sheet.

CODE LINK TO ORIGINAL POST Excel VBA Copy and Paste Loop within Loop

I have a question on how to limit the range to a 'named range' (C13:Z111) rather than the 'entire column' ("C") in this code. I cannot seem to get it to limit to copy rows, starting with last row with data and counting down to the first row.

I have some rows (C1:C12) with titles at the top and the data starts at row 13. So when copying values from one sheet to the 'other' sheet, the top rows also copy. I would like to end the copying of data at row 13.

Thank you for your help.

Here is what currently works with the exception that I am not able to limit the range.

Sub Generate_Invoice()

Dim i As Long
Dim ii As Long
Dim i3 As Long
Dim LastRow As Long
Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet

Set wb = ThisWorkbook
Set sht1 = wb.Sheets("INCENTIVE")
Set sht2 = wb.Sheets("Sheet2")

Sheets("Sheet2").Select
Range("B11:Z200").ClearContents

'Find the last row (in column C) with data.
LastRow = sht1.Range("C13:C111").Find("*", searchdirection:=xlPrevious).Row
ii = 2

'This is the beginning of the loop >>>This Works BUT BUT BUT goes all the way to the top - REQUESTING HELP WITH CODE ENDS AT ROW 13 AND DOES NOT GO PAST<<<

For i = 3 To LastRow
    'First activity
    sht2.Range("B" & ii) = sht1.Range("C" & i).Value
    sht2.Range("C" & ii) = sht1.Range("G" & i).Value
    sht2.Range("D" & ii) = sht1.Range("H" & i).Value
    sht2.Range("E" & ii) = sht1.Range("P" & i).Value
    sht2.Range("F" & ii) = sht1.Range("R" & i).Value
    sht2.Range("G" & ii) = sht1.Range("AD" & i).Value

    ii = ii + 1

Next i

'Return to "Sheet2"
Sheets("Sheet2").Select

'Add SUM at bottom of last record in Range"D"
 Dim ws As Worksheet

    For Each ws In Worksheets
        With ws.Range("F" & Rows.Count).End(xlUp).Offset(2)
            .FormulaR1C1 = "=SUM(R11C6:R[-1]C6)"
            .Offset(, -1).Value = "Total:"
        End With
    Next ws

End Sub

You were looking for the last row but only looking within the populated area. I would suggest changing the method that the last row is determined by starting at the bottom of the worksheet and finding the last populated cell in column C. This would be like being in C1048576 and tapping Ctrl + .

'Find the last row (in column C) with data.
LastRow = sht1.Cells(Rows.Count, "C").End(xlUp).Row

'not sure whether you want to reverse this as well
ii = 2

'This is the beginning of the loop >>>This Works BUT BUT BUT goes all the way to the top - REQUESTING HELP WITH CODE ENDS AT ROW 13 AND DOES NOT GO PAST<<<

For i = LastRow To 13 Step -1   'work from the bottom to the top.
    'First activity
    sht2.Range("B" & ii) = sht1.Range("C" & i).Value
    sht2.Range("C" & ii) = sht1.Range("G" & i).Value
    sht2.Range("D" & ii) = sht1.Range("H" & i).Value
    sht2.Range("E" & ii) = sht1.Range("P" & i).Value
    sht2.Range("F" & ii) = sht1.Range("R" & i).Value
    sht2.Range("G" & ii) = sht1.Range("AD" & i).Value

    'not sure whether you want to reverse this as well
    ii = ii + 1

Next i

You just need to exit the for loop based on whatever your desired criteria is. For example:

If ii = 13 Then Exit For

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM