Excel宏根据具有数据的两个单元格之间的行数插入或删除行

时间:2014-04-23 16:49:14

标签: excel vba excel-vba


我说的是一个名字有三个空白行接下来有两个,然后是四个,然后是三个,等等

我正在寻找最终结果,其中每个名称都有两个空白行

这是我目前所拥有的,并且它不起作用它只是在名字下添加一行

Dim c As Range
Dim counting As Boolean
Dim zeroCount As Long

For Each c In activeSheet.[A2:A30].Cells
    If Len(c) = 0 Then
        Exit For
    Else
        If Not c.Value2 = "" Then
            counting = True
            Do While Not zeroCount = 3
                If zeroCount < 2 Then
                    c.Offset(1, 0).EntireRow.Insert Shift:=x1Down, CopyOrigin:=xlFormatFromLeftOrAbove
                    zeroCount = zeroCount + 1
                End If
                If zeroCount > 3 Then
                    c.Offset(1, 0).Delete Shift:=x1Up
                    zeroCount = zeroCount - 1
                End If
                End
            Loop
            zeroCount = 0
        Else
            If counting Then
                zeroCount = zeroCount + 1
            End If
        End If
    End If
Next c

1 个答案:

答案 0 :(得分:0)

谢谢Acantud,这很像我想要的

  

听起来你想首先清除所有空白行,然后运行一个循环,在每行之间插入两个空白行和一个蓝色行?首先删除空白行,通过自动过滤(更快)或使用循环(更容易编码),如果单元格(x,1)=&#34;&#34;然后是行(x).delete。然后做另一个循环 - 行(x)。插入,行(x)。插入,行(x)。插入,范围(&#34; A&#34;&amp; x + 3&amp;&#34;:G&# 34;&amp; x + 3).interior.color = rgb(0,0,255),x = x + 4。看起来你已经取得了进步,在我看来,最好先清除所有空白行。如果我能提供任何其他帮助,我会 - - Acantud 19小时前

这就是我最后得到的结果

Dim cnt As Integer

Range("A2:A26").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
With activeSheet
    cnt = .Cells(.Rows.Count, "A").End(xlUp).Row
End With


For i = Range("A2").End(xlDown).Row To 3 Step -1
    'Gives rows to the last person
    If i = cnt Then
        Rows(i + 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromAbove
        Rows(i + 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromAbove
        Rows(i + 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromAbove
    End If
    Rows(i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromAbove
    Range("A" & i, "G" & i).Interior.Color = RGB(197, 217, 241)
    Rows(i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromAbove
    Rows(i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromAbove
Next i

非常感谢