2014-10-27 73 views
-1

我在Excel中有一組數據。人口以年爲單位數百萬,但是我需要填寫的數據存在差距。看下面的例子用於線性系列填充的Excel宏

Year--2013--2014--2015--2016--2017--2018--2019--2020 
Male-- 5-- -- 7-- -- -- 8-- -- 10 
Fem-- 4-- -- 5-- -- -- 7-- -- 9 

我知道我可以使用填充/系列/線性,但我需要從2000年至2050年爲數百個國家做到這一點。我試圖記錄我這樣做的宏,但Step Value似乎是硬編碼的。

這是甚至可能或我咬牙,繼續手動?

感謝

Ĵ

Sub FillTheGaps() 
' 
' fill the gaps Macro 
' 

'start at cell A2 
'find first gap on that row 
Selection.End(xlToRight).Select 
'select up to and including next non-blank 
Range(Selection, Selection.End(xlToRight)).Select 
Selection.DataSeries Rowcol:=xlRows, Type:=xlLinear, Date:=xlDay, Step _ 
    :=11845, Trend:=False 
Selection.End(xlToLeft).Select 
'move down to next row 
ActiveCell.Offset(1).Select 
Selection.End(xlToRight).Select 
'select up to and including next non-blank 
Range(Selection, Selection.End(xlToRight)).Select 
Selection.DataSeries Rowcol:=xlRows, Type:=xlLinear, Date:=xlDay, Step _ 
    :=8598, Trend:=False 
Selection.End(xlToLeft).Select 
'move down to next row 
ActiveCell.Offset(1).Select 
Selection.End(xlToRight).Select 
'select up to and including next non-blank 
Range(Selection, Selection.End(xlToRight)).Select 
Selection.DataSeries Rowcol:=xlRows, Type:=xlLinear, Date:=xlDay, Step _ 
    :=30400, Trend:=False 
'move down to next row 
ActiveCell.Offset(1).Select 
End Sub 
+0

爲什麼不告訴我們你錄製的內容?所有來自宏錄像機的*都是*硬編碼的,但通常它可以很容易地修改... – 2014-10-27 15:32:20

+0

我現在已經包含了我記錄的宏,這有幫助嗎? – jellybean1977 2014-10-28 10:38:07

+0

步數值代表什麼?如果你能確定它們是如何分配的,應該很容易把它放在一個循環中。 – 2014-10-28 13:55:50

回答

0

我想這應該讓你開始。我所做的就是創建一個循環,並根據您的解釋進行一些計算以獲取步驟值,因爲這實際上是唯一的「變量」。其餘的是Excel中的how to avoid using or relying on the Selection method練習;我創建了一個變量rng來表示數據的每一行/範圍,然後使用適當的方法來定義該範圍,而不是依靠手動用戶選擇範圍。

Dim rng As Range 
Dim stepValue As Long 

Set rng = Range("A2", Range("A2").End(xlToRight)) 

Do 
    'Compute the difference between the first & last cell in the range, 
    ' divided by the number of blank cells + 1. 
    stepValue = (rng(rng.Cells.Count).Value - rng(1).Value)/_ 
      (rng.SpecialCells(xlCellTypeBlanks).Count + 1) 

    'now we can use our computed "stepValue" instead of hard-coding it as a constant: 
    '## Use the resize method to avoid overwriting the last cell in this range 
    rng.Resize(, rng.Cells.Count - 1).DataSeries Rowcol:=xlRows, _ 
       Type:=xlLinear, _ 
       Date:=xlDay, _ 
       Step:=stepValue, _ 
       Trend:=False 


    'Increment the range to the next row 
    Set rng = Range(rng(1).Offset(1), rng(1).Offset(1).End(xlToRight)) 

'Escape the loop only when we reach an empty/blank cell in the first column: 
Loop Until Trim(rng(1).Value) = vbNullString 
+0

謝謝@David Zemens。這是循環方面的工作,但步驟值計算並不完全正確。它看起來是a)沒有被正確的數量分割(例如,如果四個空白單元格之間,那麼它應該除以5,而不是除以空白的數量,然後加上1),b)它會覆蓋最右邊的值在這個範圍內,而不是僅僅填補兩者之間的空白?謝謝J – jellybean1977 2014-10-29 09:29:37

+0

我認爲'stepValue'計算中的一些括號會幫助第一個問題。將分母'rng.SpecialCells(xlCellTypeBlanks).Count + 1'放在括號內。 – 2014-10-29 14:08:34

+0

對於第二個問題,請執行'rng.Resize(,rng.Cells.Count -1).DataSeries ...' – 2014-10-29 14:10:46