如果序列数据为空,则插入“自动值”

时间:2019-08-06 13:00:05

标签: excel vba formula

如果C列中有一个空单元格,请从头开始从B列中获取数据,并将其导入D列中。

我在D6中写道 =OFFSET($B$4:$B$13,$C$2,0,1,1) 和C2我写 =COUNTBLANK($C$4:$C$13) 但结果是App4

enter image description here

在图片上编辑enter image description here

2 个答案:

答案 0 :(得分:2)

使用:

=IF(C4="",INDEX($B$4:$B$13,SUMPRODUCT(--($D$3:D3<>""))+1),"")

enter image description here

答案 1 :(得分:1)

您可以尝试:

Option Explicit

 Sub test()

    Dim LastRow As Long, i As Long, Counter As Long

    Counter = 4

    With ThisWorkbook.Worksheets("Sheet1")

        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

        For i = 4 To LastRow

            If .Range("C" & i).Value = "" Then
                .Range("D" & i).Value = .Range("B" & Counter).Value
                Counter = Counter + 1
            End If

        Next i

    End With

 End Sub