创建一个包含不完整数据的逐步值表

时间:2016-03-20 23:48:33

标签: excel graph

我无法理解这一点。

我有一个缺少查找的值表。

Price   Company 1   Company 2   Company 3
$100        5           -           1
$200        2           9           -
$300        6           -           -
$400        -           2           1
$500        4           -           -

我想用相邻单元格的值填写这些缺失的条目,这样我仍然可以逐步绘制它们。

Price   Company 1   Company 2   Company 3
$100        5         **9**         1
$200        2           9         **1**
$300        6         **9**       **1**
$400      **6**         2           1
$500        4         **2**       **1**

逻辑是从上面的行中获取值,如果上面没有可能的行包含值,则从下面的行中获取下一个可能的值。上面的例子说明了这一点。在确定用什么填充单元格时,数字的位置比数字的实际值更重要。

1 个答案:

答案 0 :(得分:1)

这应该让你开始。

Sub Stepwise()
    'get the worksheet
    With Worksheets("sheet1")
        'get the block of data radiating out from A1
        With .Cells(1, 1).CurrentRegion
            'shift off the header row and right one column
            With .Cells.Resize(.Rows.Count - 1, .Columns.Count - 1).Offset(1, 1)

                'get rid of hyphens
                .Replace what:=Chr(45), replacement:=vbNullString, lookat:=xlWhole
                'optional - get rid of any non-numeric values
                On Error Resume Next
                .Value = .Value2
                .SpecialCells(xlCellTypeConstants, xlTextValues) = vbNullString
                On Error GoTo 0

                'shift one more row down - same number of columns
                With .Cells.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                    'make sure there are blank cells
                    If Application.Count(.Cells) < .Cells.Count Then
                        'get the blank cells
                        With .SpecialCells(xlCellTypeBlanks)
                            .FormulaR1C1 = "=R[-1]C"
                        End With
                        .Value = .Value2
                    End If
                End With

                'shorten by one row - same number of columns
                With .Cells.Resize(.Rows.Count - 1, .Columns.Count)
                    'make sure there are blank cells
                    If Application.Count(.Cells) < .Cells.Count Then
                        'get the blank cells
                        With .SpecialCells(xlCellTypeBlanks)
                            .FormulaR1C1 = "=R[1]C"
                        End With
                    End If
                    .Value = .Value
                End With

            End With
        End With
    End With
End Sub

如果逻辑不适用于较大的间隙,则尝试修改,如果卡住,请回来寻求帮助。

Stepwise_sample_data_before Stepwise_sample_data_after
Stepwise之前的样本数据 Stepwise之后的样本数据