2016-03-20 28 views
1

我無法繞過這個問題。用不完整的數據創建一個逐步數值表

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

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

這應該讓你開始。

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 

如果邏輯並不適用於較大的差距,然後嘗試修改(S)和回來尋求幫助,如果你會被卡住。逐步前

Stepwise_sample_data_beforeStepwise_sample_data_after
的樣本數據逐步

+0

感謝@Jeeped後的樣本數據。我得到一個運行時錯誤,所以我沒有真正能夠解決代碼的功能(我是VBA的新手)。 未找到單元格 - 代碼行:With .SpecialCells(xlCellTypeBlanks) – Wolfspirit

+0

該公式不適用於單元格中的公式嗎?我轉換爲值,它的工作 – Wolfspirit

+0

是的,單元格中的公式必須以不同的方式處理。請參閱上面的我的編輯以轉換爲值和所有非數字數據替換。 – Jeeped