excel 尝试循环数据并将其粘贴到另一个具有列偏移量的工作表中

p8h8hvxi  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(167)

您好,我正在尝试循环数据并将其粘贴到另一个具有列偏移量的工作表中
将数据贴到“Dynamisch voorblad”工作表后,我从表格复制了一些日期(我知道这不是正确的方式)。我将结果从(G24:G27)复制到“Formule货车Groei per week”(范围B5)
我想把这个复制到工作表“Formule货车Groei per week”,从“B5”开始,在每个循环周期后复制到下一列。所以B5 C5 D5 E5 F5等。
我试图用我的方式来实现这一点
我编写代码:

Sub ggg()

For i = 2 To 21

'kies start
Sheets("WAKA").Range("C" & i).copy
Sheets("Dynamisch Voorblad").Range("C2").PasteSpecial Paste:=xlPasteValues
'kies eind
Sheets("WAKA").Range("D" & i).copy
Sheets("Dynamisch Voorblad").Range("C3").PasteSpecial Paste:=xlPasteValues
Sheets("Dynamisch Voorblad").Range("G24:G27").copy

Sheets("Blad13").Range(i, 5).PasteSpecial xlPasteValues
Next i

End Sub
qmelpv7a

qmelpv7a1#

复制值

  • 按分配复制更有效,而且不会弄乱选择。
Option Explicit

Sub ggg()

    ' Reference the workbook.
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' Reference the worksheets.
    
    Dim wsDyn As Worksheet: Set wsDyn = wb.Worksheets("Dynamisch Voorblad")
    Dim wsWak As Worksheet: Set wsWak = wb.Worksheets("WAKA")
    Dim wsGro As Worksheet
    Set wsGro = wb.Worksheets("Formule van Groei per week")

    ' Reference the ranges.
    
    Dim rgDyn As Range: Set rgDyn = wsDyn.Range("G24:G27")
    Dim fCellGro As Range: Set fCellGro = wsGro.Range("B5")
    ' Make it the same size as 'rgDyn'.
    Dim rgGro As Range: Set rgGro = fCellGro.Resize(rgDyn.Rows.Count)

    Dim i As Long
    
    ' Loop.
    For i = 2 To 21
        'kies start
        wsDyn.Range("C2").Value = wsWak.Cells(i, "C").Value
        'kies eind
        wsDyn.Range("C3").Value = wsWak.Cells(i, "D").Value
        ' Copy the range.
        rgGro.Value = rgDyn.Value
        ' Reference the next column range.
        Set rgGro = rgGro.Offset(, 1)
    Next i

End Sub

相关问题