Excel VBA Copy and Paste Loop within Loop

后端 未结 2 581
一个人的身影
一个人的身影 2020-12-11 11:28

I’ve been working on this VBA code for a while and since I’m a complete noob I feel like I haven’t gotten anywhere. I’ve been researching a ton, but can’t seem to combine an

相关标签:
2条回答
  • 2020-12-11 11:41

    The way I usually approach copying data between sheets in Excel is to create source range and destination range objects, each range referring to just one row. When I want to move on to the next row, I use Offset to return a range offset to the next row.

    Since the ranges only refer to one row, you can index them with an integer to get the cells in the row. E.g. if cursor refers to columns A through D in row 3, cursor(3) will give you the cell C3.

    Dim src_cursor As Range
    Dim dest_cursor As Range
    
    Set src_cursor = ActiveSheet.Range("A3:I3")
    Set dest_cursor = Sheets("Sheet2").Range("A2:D2")
    
    '' Loop until column A is empty in source data
    Do Until IsEmpty(src_cursor(1))
        dest_cursor(1) = src_cursor(6) '' copy F -> A
        dest_cursor(2) = src_cursor(4) '' copy D -> B
        '' and so on
    
        '' move cursors to next row
        Set src_cursor = src_cursor.Offset(1, 0)
        Set dest_cursor = dest_cursor.Offset(1, 0)
    Loop
    

    Also, this might be getting a little off topic, but it's a better practice to use an Enum to name the column numbers instead of hardcoding them like I did here.

    0 讨论(0)
  • 2020-12-11 11:53

    There are many ways to do this, and some are more efficient than others. My solution may not be the most efficient, but hopefully it will be easy for you to understand so that you can learn.

    It's very difficult to understand what you're attempting to do in activity three, so I wasn't able to provide a solution to that step. Use my code as a template for step three and if you run into issues, feel free to leave a comment.

    Notice that I don't use .Activate, .Select, or .Copy in this code. .Activate and .Select are huge efficiency killers, and they make it easier for your code to "break," so avoid using them when possible. .Copy isn't necessary when working with values or formulas and will also slow your code down.

    Untested

    Sub testLoopPaste()
    
    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("Sheet1")
    Set sht2 = wb.Sheets("Sheet2")
    
    'Find the last row (in column A) with data.
    LastRow = sht1.Range("A:A").Find("*", searchdirection:=xlPrevious).Row
    ii = 2
    
    'This is the beginning of the loop
    For i = 3 To LastRow
        'First activity
        sht2.Range("A" & ii) = sht1.Range("F" & i).Value
        sht2.Range("B" & ii) = sht1.Range("D" & i).Value
        sht2.Range("C" & ii) = sht1.Range("A" & i).Value
        sht2.Range("D" & ii) = sht1.Range("H" & i).Value
        ii = ii + 1
    
        'Second activity
        sht2.Range("A" & ii) = sht1.Range("F" & i).Value
        sht2.Range("B" & ii) = sht1.Range("D" & i).Value
        sht2.Range("C" & ii) = sht1.Range("A" & i).Value
        sht2.Range("D" & ii) = sht1.Range("I" & i).Value
        ii = ii + 1
    
        'Third activity
        For i3 = 1 To sht1.Range("K" & I)  
            sht2.Range("A" & ii) = sht1.Range("F" & i).Value  
            sht2.Range("B" & ii) = sht1.Range("D" & i).Value  
            sht2.Range("C" & ii) = sht1.Range("A" & i).Value  
            ii = ii + 1  
        Next i3
    
    Next i
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题