Excel VBA - 循环增量

时间:2017-12-13 02:12:02

标签: excel-vba vba excel

如何从源表中每四行引用一个单元格,然后将其放在输出表中?每个单元格都有不同的值。提前谢谢

1 个答案:

答案 0 :(得分:1)

这应该可以胜任。在您在工作簿中尝试之前,请确保进行必要的自定义。

Sub TransferData()

    Dim WsS As Worksheet, WsT As Worksheet      ' Source & Target
    Dim Rl As Long                              ' Last row
    Dim Rs As Long, Cs As Long                  ' Source: Row, Column
    Dim Rt As Long, Ct As Long                  ' Target: Row, Column

    Set WsS = Worksheets("Source")              ' change name as required
    Set WsT = Worksheets("Source")              ' I used the same sheet for my test

    With WsT
        Ct = 4                      ' specify the column to write to (here column D)
        Rt = .Cells(.Rows.Count, Ct).End(xlUp).Row + 1
    End With

    Application.ScreenUpdating = False
    With WsS
        Cs = 1                      ' specify the column to read from (here column A)
        Rl = .Cells(.Rows.Count, Cs).End(xlUp).Row
        For Rs = 2 To Rl Step 4                 ' start from row 2
            WsT.Cells(Rt, Ct).Value = .Cells(Rs, Cs).Value
            Rt = Rt + 1
        Next Rs
    End With
    Application.ScreenUpdating = True
End Sub