Excel VBA循环逻辑

我的脑袋疼。

丑陋的道歉 – 我通常学习和使用VBA只是因为我需要它,所以我的语言口才有点欠缺。

我一直在试图想出一种方式来循环我的行,并插入一行时,发现一个特定的值 – 不要笑,我没有完成 – 但我所寻找的价值不一定去那里。

我正在testing的数据是A列中的“Days Old” – 我需要插入一行(上图),并使用公式填充列B到lCol ,以便在第15天将date总和返回到上一个目标日, lRow -1lRow -1 (可能是180+)

这是我得到代码的地方(最后把第二部分搞乱了,不考虑这个问题):

 With Sheets("Output") lRow = Range("A" & Rows.Count).End(xlUp).Row lColi = Cells.Find("*", SearchOrder:=xlByColumns, _ LookIn:=xlValues, SearchDirection:=xlPrevious).Column lCol = Split(Cells(2, lColi).Address, "$")(1) For i = lRow To 3 Step -1 If Cells(i, 1).Value = "15" OR _ Cells(i, 1).Value = "30" OR _ Cells(i, 1).Value = "60" OR _ Cells(i, 1).Value = "90" OR _ Cells(i, 1).Value = "120" OR _ Cells(i, 1).Address = Range("A" & lRow -1) Then ' Not quite sure on that one yet either.. Rows(i).Select Selection.Insert Shift:=xlDown ' Part 2 (Included just in case I delete it from the Module in a rage later tonight) ' Cells(i,1).Offset(-1,0).Select ' Selection.Value = [Date Range. Worrying about this later] 'Cells(i,1).Offset(-1,1).Select 'ActiveCell.FormulaR1C1 = "=SUM(R[-" & n & "]C:R[-1]C)" ' Where n is somehow the offset to +1 from the previous target.... but again, later. 'ActiveCell.NumberFormat = "0" 'Selection.AutoFill Destination:=ActiveCell.Range("C" & i & ":" & lCol & i), Type:= _ xlFillDefault 

这里是我身边的刺(编辑:想象一下,我有口碑点,这是我准备的Excel表中的便捷图片):

 [A] .. 88 89 91 92 .. 

正如你所看到的,我并不总是得到一个输出结果,我需要查找实际的日数(在上面的例子中是90)。

所以; 有没有一种方法,我可以循环通过我的行,并插入一个新的一个在我的目标日或前一天如果没有出现的最近的?

我真的怀疑这是否可能成为某种巧妙使用Case的候选人(按照Adam Ralph的回答),但放弃了试图在第五个小时试图把它在家里搞定的想法。

如果你能帮我find一个优雅的解决scheme,我将非常感激!

[编辑]:

@mehow,最终结果通常是这样的,91天以上的行显示插入行的新数据:

A =“(上一个目标) – (下一个目标-1)”B-lCol = sum(上一个目标:目标-1)

 [A] [B] [C] [D] ... 87 18 88 1 2 89 3 "60 - 89" 5 3 59 91 1 92 1 2 ... 

很明显,在这种情况下,我可以去另一个方向寻找89 – 但是,不能保证这将被包含在我工作的工作表中。

@Glh – 日子总是在上升。