查找列数据中的差距

时间:2016-11-29 18:41:25

标签: vba excel-vba excel

我已经看到很多问题要求找到列数据中的差距,但是还没有能够转换答案以满足我的需求。我是业余爱好者,因为vba只是为了让我的生活充满挑战!

我在Excel文件中有一列数字数据。该列包含单个和多个连续的空白单元格。我需要找到空位置并使用Excel函数用线性系列填充函数填充数据间隙。提前感谢任何指导。

到目前为止,我的代码是

Dim lngLastRow As Long

lngLastRow = Cells(Rows.Count, "F").End(xlUp).Row   'Search Column F, change as required.    

For Each cell In Range("F3:F" & lngLastRow) 'Starting cell is F3       
   If IsEmpty(Range("F3").Value) = True Then

  'store this row and check is next cell is blank & apply fill process      

   End If
Next cell

1 个答案:

答案 0 :(得分:1)

希望这段代码能实现线性填充:

Sub LinearFill()
    Dim lngLastRow As Long
    Dim endRow As Long
    Dim startValue As Double
    Dim endValue As Double
    Dim thisRow As Long

    With ActiveSheet
        lngLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row   'Search Column F, change as required.
        For thisRow = 3 to lngLastRow - 1
            If IsEmpty(.Cells(thisRow, "F")) Then
                startValue = CDbl(.Cells(thisRow - 1, "F").Value)
                endRow = .Cells(thisRow, "F").End(xlDown).Row
                endValue = CDbl(.Cells(endRow, "F").Value)
                .Cells(thisRow, "F").Value = startValue + (endValue - startValue) / (endRow - thisRow + 1)
            End If
        Next
    End With
End Sub