根据列值将列内容重复“ n”行

时间:2019-10-10 17:24:25

标签: excel vba

我想根据“数字”号重复ID号。例如:

enter image description here

enter image description here

到目前为止,我已经尝试了以下方法。

  Sub MySub()
  Do While B2 = n
    CurrentSheet.Range("a1:c1").EntireRow.Resize(n).Insert
  Loop
  End Sub

这可能没有多大意义,因为我还很新!

1 个答案:

答案 0 :(得分:2)

如果要在D列中列出数据,可以使用此

Sub x()

Dim r As Range

For Each r In Range("A2", Range("A" & Rows.Count).End(xlUp))                         'loop through A
    Range("D" & Rows.Count).End(xlUp)(2).Resize(r.Offset(, 1).Value).Value = r.Value 'duplicate number of times in B
Next r

End Sub

如果要插入现有数据中

Sub x()

Dim r As Long

For r = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
    If Cells(r, 2) > 1 Then
        Cells(r + 1, 1).EntireRow.Resize(Cells(r, 2).Value - 1).Insert shift:=xlDown
        Cells(r + 1, 1).Resize(Cells(r, 2).Value - 1) = Cells(r, 1).Value
    End If
Next r

End Sub