我有2次循环,效果不佳

时间:2019-05-30 12:01:12

标签: vba loops

我有2个循环,但不幸的是它无法正常工作。我想每隔50行取第一个特定单元格,然后每隔31行将其粘贴到另一个选项卡中。不幸的是,就目前而言,我仅复制一个数据范围并将其粘贴到第二个选项卡中,重复332次。我知道它的结果,但是我不知道该如何写。

Sub makro3()

    Dim X As Integer, X_max As Integer

    X_max = 332

    Y_max = 10292

    For X = 0 To X_max Step 50

        Sheets("Arkusz1").Select
        Cells(14 + X, 3).Select
        ActiveCell.Range("A1:T31").Select
        Selection.Copy
        Sheets("dane").Select

        For Y = 0 To Y_max Step 31
            Cells(2 + Y, 7).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
        Next Y

    Next X

End Sub

2 个答案:

答案 0 :(得分:0)

尝试一下:

Option Explicit

Sub makro3()

    Dim X As Long, Y As Long
    Dim X_max As Long: X_max = 332
    Dim Y_max As Long: Y_max = 10292

    For X = 0 To X_max Step 50

        ThisWorkbook.Sheets("Arkusz1").Cells(14 + X, 3).Copy

        With ThisWorkbook.Sheets("dane")

            For Y = 0 To Y_max Step 31
                .Cells(2 + Y, 7).PasteSpecial Paste:=xlPasteValues
            Next Y

        End With

    Next X

End Sub

答案 1 :(得分:0)

Sub makro3() 
Dim X As Integer, X_max As Integer
Dim Y As Integer, Y_max As Integer
Dim Z_max As Double



X_max = 16564
Y_max = 10292
Z_max = X_max / 50

For Z = 0 To Z_max
    X = Z * 50
    Y = Z * 31
    Sheets("Arkusz1").Select
    Cells(14 + X, 3).Select
    ActiveCell.Range("A1:T31").Select
    Selection.Copy
    Sheets("dane").Select

    Cells(2 + Y, 7).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
Next Z

End Sub