【问题标题】:Copy & Paste Cell Value "X" amount of times then loop to next row复制和粘贴单元格值“X”次然后循环到下一行
【发布时间】:2020-08-20 13:09:10
【问题描述】:

详情:

  1. 如果 YR 值为 1,则复制 D 列中的单元格值,从 G 列开始粘贴
  2. 这会在每一行中重复计数器 # 次 IE 如果计数器为 5,则将单元格值从 G 复制并粘贴到 K
  3. 一旦计数器过期,移动到下一行并重复过程

Excel 截图

Dim StartRow As Byte
Dim LastRow As Long
Dim i As Integer
Dim ii As Integer
Dim cnt As Integer

StartRow = 3
LastRow = Range("B3").CurrentRegion.Rows.Count - 1

For i = StartRow To LastRow
    cnt = Range("C" & i).Value
    For ii = 1 To cnt
        If Range("B" & StartRow) = 1 Then
            Range("D" & StartRow).Copy
            Range("B" & StartRow).End(xlToRight).Offset(0, ii + 2).PasteSpecial (xlPasteAll)
        End If
    Next ii
Next i

【问题讨论】:

  • 你有什么问题?
  • 如何根据计数器值复制和粘贴多次,然后循环几行(仅在 YR 1 时才这样做)。 Mazaffer GALATA 的代码似乎可以工作。

标签: excel vba for-loop


【解决方案1】:

你可以试试这个:

Dim StartRow As Byte
Dim LastRow As Long
Dim i As Integer
Dim ii As Integer
Dim cnt As Integer
Dim limitCol As Integer

StartRow = 3
LastRow = Range("B3").CurrentRegion.Rows.Count - 1

For i = StartRow To LastRow
    If (CInt(Range("B" & i)) = 1) Then
        cnt = Range("C" & i).Value
        limitCol = cnt + 6
        For ii = 7 To limitCol
            Cells(i, ii) = Range("D" & i).Value
        Next ii
    End If
Next i

【讨论】:

  • 根据一些回溯测试,这似乎工作正常。谢谢。
猜你喜欢
  • 2014-12-27
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-21
  • 2020-08-03
相关资源
最近更新 更多