Results 1 to 6 of 6

Thread: Want to add new rows depending on a variable, and increment cell contents by 1

  1. #1
    New Member
    Join Date
    Sep 12
    Posts
    4

    Question Want to add new rows depending on a variable, and increment cell contents by 1

    Right, so basically I want to average daily prices of a commodity for a giving pricing period.

    However, the period changes.

    What I'm envisioning is as such: I tell excel the START DATE on cell B3, END DATE on B4, and B5 is "=B4-B3+1" and that gives the number of days (counting inclusively, hence the +1). So for example, for a pricing period of Sep10-13, that's 4 days, so B5 = 4.

    Now, using the same example, I want a macro that adds values in the B column, starting from B7, as such:

    Sep-10
    Sep-11
    Sep-12
    Sep-13

    So basically it's +1 day all the time until it reaches the END DATE (or until it increments to the value in B5 MINUS 1 --- that should be the cap.... i.e. notice how it's +0, then +1, +2 and finally +3).



    Should the period be changed to Sep12-13, for example, excel would automatically remove the last 2 rows and their contents, and starting from B7 only the following would be shown:

    Sep-12
    Sep-13

    (and the value in B5 would be 2).




    Does that make sense?





    I've been trying to do this but I am getting nowhere... I am sure one of you guys can help me out?

    Thanks!!





    After that is all done, I would manually input all the price indexes for each day on column C (starting from C7).

    The average is calculate on cell D3. The formula is basically the sum of all the rows with price indexes (not sure how to get this to automatically expand or contract, so I'm using the entire C column) and then divides it by =COUNTA of the entire C column (counts everything that isn't blank). If you know of a cleaner/better way to do this, expanding and contracting the C column as it goes, that'd be nice, otherwise this is fine. I'm more worried about the above problem (inserting rows and incrementing).



    Thanks a lot peeps!!

  2. #2
    New Member
    Join Date
    Sep 12
    Posts
    4

    Re: Want to add new rows depending on a variable, and increment cell contents by 1

    Sorry guys I didn't realise I can't edit posts - I forgot to say that this is for MS EXCEL obviously (in Office 2010).

    Thanks peeps

  3. #3
    Fanatic Member
    Join Date
    Sep 12
    Location
    To the moon and then left
    Posts
    577

    Re: Want to add new rows depending on a variable, and increment cell contents by 1

    Code for Change-Event of the Worksheet-Object.
    You'll note that i don't use the Value in B5

    vb Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2. Dim i As Long
    3. Dim Difference As Long
    4.  
    5.     If (Target.Row = 3 And Target.Column = 2) Or (Target.Row = 4 And Target.Column = 2) Then
    6.    
    7.         Difference = DateDiff("d", CDate(Me.Cells(3, 2)), CDate(Me.Cells(4, 2)))
    8.        
    9.         If Difference >= 0 Then
    10.        
    11.             For i = 0 To Difference
    12.            
    13.                 Me.Cells(i + 7, 2) = DateAdd("d", i, Me.Cells(3, 2))
    14.            
    15.             Next
    16.        
    17.         End If
    18.    
    19.     End If
    20.  
    21. End Sub
    For health reasons i try to avoid reading unformatted Code

  4. #4
    New Member
    Join Date
    Sep 12
    Posts
    4

    Re: Want to add new rows depending on a variable, and increment cell contents by 1

    Oh wow, thanks a lot Zvoni, it works!

    As I suspected, the code is much simpler than what I was trying to do.



    Could I ask you if it's possible to make one small addition? Right now, if the range given is 5 days, it will correctly add 5 date values along 5 rows staring from B7. If this is later changed to a SHORTER period, e.g 3 days only, it will also correctly do it, however the other 2 rows (the values in B10 and B11) will not be cleared and show as blank - instead they will have the "leftover" values from the previous period.

    I'm thinking that the best way to do this is to run a script that before any new calculation, it'll clear the entire column B of ANY values, starting from B7. Can you add that to the script?

    Thanks a lot!!

  5. #5
    Fanatic Member
    Join Date
    Sep 12
    Location
    To the moon and then left
    Posts
    577

    Re: Want to add new rows depending on a variable, and increment cell contents by 1

    vb Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2. Dim i As Long
    3. Static Difference As Long     '##### Changed from Dim to Static
    4.  
    5.     If (Target.Row = 3 And Target.Column = 2) Or (Target.Row = 4 And Target.Column = 2) Then
    6.         '#### Additional Loop to clear the cells
    7.         For i = 0 To Difference
    8.        
    9.             Me.Cells(i + 7, 2) = ""
    10.        
    11.         Next
    12.        
    13.         Difference = DateDiff("d", CDate(Me.Cells(3, 2)), CDate(Me.Cells(4, 2)))
    14.        
    15.         If Difference >= 0 Then
    16.        
    17.             For i = 0 To Difference
    18.            
    19.                 Me.Cells(i + 7, 2) = DateAdd("d", i, Me.Cells(3, 2))
    20.            
    21.             Next
    22.        
    23.         End If
    24.    
    25.     End If
    26.  
    27. End Sub
    For health reasons i try to avoid reading unformatted Code

  6. #6
    New Member
    Join Date
    Sep 12
    Posts
    4

    Re: Want to add new rows depending on a variable, and increment cell contents by 1

    Thanks man it works wonderfully, you rock!! THANKS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •