选择并填充两个空行

时间:2018-05-05 13:04:17

标签: excel vba excel-vba

我一般都是Macros和VBA的新手。我到处搜索,找到解决问题的方法,但我找不到任何问题。我有以下格式的以下数据:

A    B    C
    ...   Text1
    ...   Text1
    ...   Text1
    ...   Text1
--- empty row
    ...   Text2
    ...   Text2
    ...   Text2
--- empty row
    ...   Text3
    ...   Text3
--- empty row

A列全为空。我想像这样填写A:

    A    B    C
    1   ...   Text1
    2   ...   Text1
    3   ...   Text1
    4   ...   Text1
--- empty row
    1   ...   Text2
    2   ...   Text2
    3   ...   Text2
--- empty row
    1   ...   Text3
    2   ...   Text3
--- empty row

基本上,从1-x开始计数,直到C中的值变为空行。

3 个答案:

答案 0 :(得分:1)

如果需要计算“C”行中的填充单元格,可以使用此代码。

dim counter as integer
counter=1
For i=1 to activesheet.usedrange.rows.count
if not Range("C" & i).value="" Then
    Range("A" & i).value=counter
    counter=counter+1
else
    counter=1
end if
next i

答案 1 :(得分:0)

将此公式键入A2单元格并向下拖动:

=IF(ISBLANK(C2),"",IF(OR(ROW(C1)=1,ISBLANK(C1)),1,A1+1))

答案 2 :(得分:0)

您可以使用特殊单元格



Sub Test()
    Dim myAreas     As Areas
    Dim i           As Long
    Dim x           As Long
    
    Set myAreas = Columns("C").SpecialCells(2).Areas
    
    For i = myAreas.Count To 1 Step -1
        For x = 1 To myAreas(i).Count
            myAreas(i)(x).Offset(, -2).Value = x
        Next x
    Next i
End Sub




相关问题