如果范围内的单元格不为空,请在下面插入新行

时间:2019-01-17 18:07:00

标签: excel vba

我的数字和字母在B2:B60范围内。

如果单元格B2:B60中有数据,我想在下面插入一行。

因此它看起来像:

   A B
1    e -> EntireRow.Insert below
2    
3    3 -> EntireRow.Insert below
4    a -> EntireRow.Insert below
5    
6    er -> EntireRow.Insert below
7    
8    w -> EntireRow.Insert below

我尝试过:

Dim Rng As Range
For Each Rng In Range("B2:B60")
    If Is Not Empty (Rng.Value) Then
        Rng.Offset(1, 0).EntireRow.Insert
    End If
Next

2 个答案:

答案 0 :(得分:1)

如果不是IsEmpty

Option Explicit

Sub EmptyRows()

    Dim Rng As Range

    For Each Rng In Range("B2:B60")
        If Not IsEmpty(Rng) Then
            Rng.Offset(1, 0).EntireRow.Insert
        End If
    Next

End Sub

答案 1 :(得分:1)

可能需要反转循环,以防止在插入新行时数据在执行过程中移出输入范围。不确定For Each循环如何处理类似的事情。

Sub Main()

Dim R As Range
Set R = Range("Sheet1!C4:C20")
Dim FR As Integer
Dim LR As Integer

FR = 1 ' First Row in R
LR = R.Rows.Count ' Last Row in R

Dim Index As Integer

For Index = LR To FR Step -1
    If Not IsEmpty(R(Index)) Then
        R(Index).Offset(1, 0).EntireRow.Insert
    End If
Next

End Sub
相关问题