根据单元格中的设置编号添加行

时间:2018-04-03 17:24:07

标签: excel vba

如何调整下面的代码,使其仅适用于活动单元格,而不适用于列A中的所有单元格。否则,代码可以正常工作。 谢谢!

Public Sub Insert()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual   'pre XL97 xlManual

lastrow = ActiveSheet.UsedRange.Rows.Count
ActiveSheet.Cells(lastrow, 1).Select

Set CurrentCell = ActiveSheet.Cells(lastrow, 1)

For n = lastrow To 0 Step -1
    If n = lastrow Then GoTo CheckLastRow
    If n = 1 Then GoTo CheckfirstRow
        ActiveCell.Offset(-2, 0).Select

CheckLastRow:
    Set NextCell = CurrentCell.Offset(-1, 0)

        ActiveCell.Offset(1, 0).Select
        For i = 1 To CurrentCell
            ActiveCell.EntireRow.Insert
        Next i

    Set CurrentCell = NextCell
Next n

'To be performed on the firstrow in the column
CheckfirstRow:
        ActiveCell.Offset(-1, 0).Select
        For i = 1 To CurrentCell
            ActiveCell.EntireRow.Insert
        Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:1)

如果您想要完成的是您在评论中描述的内容,则此代码应该适合您:

Public Sub Insert()

r = ActiveCell.Value
Set myRange = ActiveCell.Offset(1, 0).EntireRow

For i = 1 To r
    myRange.Insert Shift:=xlDown
Next i

End Sub