VBA Excel 定期粘贴到列中

问题描述 投票:0回答:2

我在范围中定义了相同的数据集,我希望将其填充到多个列中直至末尾。

通过以下线程: 在 VBA Excel 中复制并粘贴多个单元格和行

我用了这个公式:

   Dim l As Long
   For l = 23 To 43 Step 6
   Cells(l, 15).PasteSpecial xlPasteAll
   Next l

更新:

我也尝试过:

   Dim l As Long
   For l = Range("W1").Column To Range("AQ1").Column Step 6
   Rows(13).PasteSpecial xlPasteAll
   Next l

但是没有成功

我怎样才能跨列(每第六列)制作常规副本?

excel vba
2个回答
0
投票

此代码将一系列单元格复制到特定列,并在它们之间定义一个定义的步骤。如果范围内有多于一列,则目标范围左上角的单元格将是起始行和定义的列的交集

Sub colcopy()

Const startcol = "C"  'this is the first column's letter to paste
Const stopcol = "X"   'this is the last column's letter to paste
Const period = 6      'the value of the column's step
Const firstrow = 8    'the first cell's row in a column to paste
Set copyrange = Range("A1:A6")  'the range to copy
copyrange.Copy

startcolvalue = Columns(startcol).Column
stopcolvalue = Columns(stopcol).Column
For i = startcolvalue To stopcolvalue Step period
Cells(firstrow, i).PasteSpecial xlPasteAll


Next i

End Sub

0
投票
Option Explicit

Sub Demo()
    Dim sRng As Range, i As Long
    Dim endCol As Long, iRow As Long
    Const SRC_RANGE = "B13:E22" ' Source range
    Const ISTEP = 6 ' interval is 6 cols
    Const END_COL = "AF" ' or 32
    ' Get the end col #
    endCol = Cells(1, END_COL).Column
    ' Get the source range
    Set sRng = Range(SRC_RANGE)
    iRow = sRng.Row
    ' Loop through and copy it over
    For i = sRng.Column + ISTEP To endCol Step ISTEP
        sRng.Copy Cells(iRow, i)
    Next
End Sub

微软文档:

范围.复制方法(Excel)

© www.soinside.com 2019 - 2024. All rights reserved.