繁体   English   中英

根据单元格值将行添加到工作表

[英]Add lines to sheet depending on cell value

我正在尝试添加等于B列Sub AddRows()中的值的空行

Dim cell As Range, numberRange As Range
Dim i As Integer, j As Integer
Set numberRange = Range("B2:B5")

For Each cell In numberRange
    Set i = 1
    Set j = Int(cell.Value)
    For i = 1 To j
        cell.EntireRow.Insert xlDown
    Next i
Next cell

End Sub

例如,如果B2中的值为2,则我希望在行3:4中添加两个空白行。

是的,PatricK是正确的。 您应该从范围的底部开始插入,以使新行不会被现有行数所困扰。

Sub AddRows()

    Dim R As Long
    Dim i As Integer

    Application.ScreenUpdating = False
    For R = 5 To 2 Step -1
        With Cells(R, 2)
            i = int(Val(.Value))
            If i Then
                .Offset(1).Resize(i, 1).EntireRow.Insert
            End If
        End With
    Next R
    Application.ScreenUpdating = True
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM