1

My brain hurts.

Part 1 of 2. Apologies for the ugly - I typically learn and use VBA only as I need it, so my eloquence in the language is a little lacking.

I've been trying to come up with a way to loop through my rows and insert a row when a particular value is found - don't laugh, I'm not finished - BUT the value I'm looking for isn't necessarily going to be there.

The data I'm testing on is 'Days Old' in Column A - I need to insert a row (above) and fill Columns B through to lCol with a formula to get the sum of days back to the previous target day at days 15, 30, 60, 90, 120, 150 and lRow -1 (which would be 180+)

Here's where I got to with the code (with Part 2 messily thrown in at the end and paying no regard to the issue at hand):

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

And here's the thorn in my side (Edit: Just imagine I have reputation points and that this is the handy pic of my excel sheet I had prepared):

[A] 
.. 
88 
89 
91 
92 
..

As you can see, I don't always get an output with the actual day number (90, in the example above) that I need to look for.

So; is there a way I can loop through my rows and insert a new one at my target day or the nearest immediately preceding day if it doesn't appear?

I did wonder if this might be a candidate for some kind of clever use of Case (as per Adam Ralph's answer here) but gave up trying to think it through sometime into the fifth hour trying to nut this one out at home.

If you can help me find an elegant solution, I'd be very grateful!

[Edit]:

@mehow, the finished result would normally be something like this, with the row above 91 days demonstrating the new data at the inserted line:

A = "(previous target) - (next target-1)" B - lCol = sum(previoustarget:target-1)

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

Obviously in this case I could go in the other direction and look for 89 - but again, there's no guarantee that this will be included on the sheet I'm working from.

@Glh - the days are always ascending.

Community
  • 1
  • 1
  • so what happens if you are looking for 90. there is 89 and 91 - which is closer in your case? and can you show an example of what your output is going to look like? also, i think you need to expand this : "- I need to insert a row (above) and fill Columns B through to lCol with a formula to get the sum of days back to the previous target day at days 15, 30, 60, 90, 120, 150 and lRow -1 (which would be 180+)" –  Mar 18 '13 at 10:17
  • Are your days always ascending? And are you only interested in the set of days above if the don't exist? – glh Mar 18 '13 at 11:27
  • Please would you try editing your question again to show what you want as output (I tried but gave up !). You should start each line with 4 spaces (to display in fixed format) and separate the columns with spaces, not tabs. – grahamj42 Mar 18 '13 at 20:27
  • Done. Note to self - always check the preview! – collinvanuden Mar 18 '13 at 20:54

0 Answers0