我对Vba真的很陌生,所以我尝试学习一些方法以减少工作重复性
我有一个具有这种格式的列表,我大约有550行
Name N
AAAAA 2
BBBB 5
CCCCC 7
我需要复制每个姓名n次,
AAAAA
AAAAA
BBBBB
BBBBB
BBBBB
BBBBB
BBBBB
直到姓氏,它应该大约为1500行
找不到任何方法可以做到这一点。请帮助
预先感谢
答案 0 :(得分:1)
这个简单的代码可以做到:
Sub expand()
With Worksheets("Sheet2") 'Source sheet
Dim rng As Variant
rng = .Range(.Cells(2, 1), .Cells(.Rows.Count, 2).End(xlUp)).Value
Dim oRng() As Variant
ReDim oRng(1 To Application.Sum(Application.Index(rng, 0, 2)), 1 To 1)
End With
Dim i As Long
Dim k As Long
k = 1
For i = 1 To UBound(rng, 1)
Dim j As Long
For j = 1 To rng(i, 2)
oRng(k, 1) = rng(i, 1)
k = k + 1
Next j
Next i
With Worksheets("Sheet2").Range("C1") 'Ouput range
.Resize(UBound(oRng, 1), 1).Value = oRng
End With
End Sub