我想在C列和D列中添加n个单元格,其中n是B列中的值。
我的代码如下:
Sub mycode()
Dim lastrow1 As Long
lastrow1 = Range("A" & Rows.Count).End(xlUp).Row
Dim lastrow2 As Long
lastrow2 = Range("C" & Rows.Count).End(xlUp).Row
For h = 2 To lastrow1
For i = 2 To lastrow2
If Sheet1.Cells(h, 1).Value = Sheet1.Cells(i, 3).Value Then
P = 10
t = i + 1
For j = 1 To Cells(i, 2).Value
Sheet1.Cells(t, 3).Insert shift:=xlDown
Sheet1.Cells(t, 4).Insert shift:=xlDown
Sheet1.Cells(t, 3).Value = Sheet1.Cells(i, 3).Value
Sheet1.Cells(t, 4).Value = Sheet1.Cells(i, 4).Value + P
P = P + 10
t = t + 1
Next j
End If
Next i
Next h
End Sub
示例输入1:
运行上述代码后,输出为:
问题: 现在,当我在第3行添加第二个条目并运行代码时,出现了一些问题:
输入:
代码生成的错误输出:
正确的输出应该是:
我无法弄清楚我的代码出了什么问题。请帮忙解决。
编辑后的样本2:
输入:
输出: