2014-10-27 65 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