+ Reply to Thread
Results 1 to 2 of 2

Aging Function

Hybrid View

  1. #1
    Forum Contributor Lotus123's Avatar
    Join Date
    11-07-2005
    Location
    Texas
    MS-Off Ver
    2007
    Posts
    153

    Aging Function

    About a year ago I had someone help me with a re-aging function. It essentially took a bad aging report and fixed it, via a function.

    For example, if I had these values:
    Current	31-  60	61-  90	91-  120	121 & Over Future
    (373.00)	0.00 	0.00 	472.00 	0.00   0.00
    It would take the current and net it against the 91-120 to have only 99.00 in the 91-120. The code includes stuff for some odd situations, and apparantly there is a new one this year that throws off the calculation.

    If I have these values:
    Current	31-  60	61-  90	91-  120	121 & Over	Future
    (128.80)	116.04 	0.00 	0.00 	0.00 	0.00
    The function is returning -128.80 in Current instead of -12.76 in current.

    I believe it has something to do with the total amount due being less than zero and it throws off the calculation.

    The way I use the above function is actually like an array...I would enter this formula into 6 cells and confirm with CTRL-SHIFT-ENTER:
    =IF($S2=1,aging($I2:$N2),"")

    Can someone review the code and possibly identify where it needs to be tweaked? I greatly appreciate it (the code grew beyond my comprehension last year, so I have difficulty figuring out exactly what it is doing).

    Thanks!

    Function Aging(rg As Range)
    Dim temp() As Variant
    ReDim temp(1 To rg.Columns.Count)
      For i = 1 To rg.Columns.Count
          temp(i) = rg(i)
      Next
      themin = Application.Min(rg)
      If themin >= 0 Then
          Aging = rg
          Exit Function
      End If
          'assumes 1 negative
          'if there are >1 positive with future=0 then apply current right-to-left
          If temp(1) = 0 And temp(2) < 0 And Application.CountIf(rg, ">0") > 1 Then
    Again:
            For i = 6 To 3 Step -1
                If temp(i) > 0 Then
                    If Abs(temp(2)) < temp(i) Then
                        temp(i) = temp(i) + temp(2)
                        temp(2) = 0
                        GoTo 1
                    Else
                        temp(2) = temp(2) + temp(i)
                        temp(i) = 0
                        GoTo Again
                    End If
                End If
            Next
          End If
          leftover = Application.Sum(temp)
          If leftover > 0 Then
              If temp(1) > 0 Then
                  For i = 2 To rg.Columns.Count
                      temp(i) = 0
                  Next
                  temp(1) = leftover
              Else
                  For i = 2 To rg.Columns.Count
                      If temp(i) > 0 Then temp(i) = leftover Else temp(i) = 0
                  Next
              End If
          Else
              For i = 2 To rg.Columns.Count
                  If temp(i) < 0 Then temp(i) = leftover Else temp(i) = 0
              Next
          End If
          For i = 2 To 5
               If temp(i) <> 0 Then
               For j = i + 1 To 6
                   temp(j) = 0
               Next
               End If
          Next
          n = 0
          For i = 1 To 6
               If temp(i) <> 0 Then n = n + 1
           Next
           If n > 1 Then temp(1) = 0
    1:
      Aging = temp
    End Function
    One interesting note:

    Although this doesn't work right:
    Current	31-  60	61-  90	91-  120	121 & Over	Future
    (128.80)	116.04 	0.00 	0.00 	0.00 	0.00
    If you change it around a bit it will work right:
    Current	31-  60	61-  90	91-  120	121 & Over	Future
    116.04 (128.80)	0.00 	0.00 	0.00 	0.00
    Last edited by Lotus123; 02-29-2008 at 01:22 PM.
    Ecce Potestas Casei
    Nathan Head

  2. #2
    Forum Contributor Lotus123's Avatar
    Join Date
    11-07-2005
    Location
    Texas
    MS-Off Ver
    2007
    Posts
    153
    Okay...with a really poor understanding of what this function is doing behind the scnenes I was able to inch my way through the code.

    Snippit:
          leftover = Application.Sum(temp)
          If leftover > 0 Then
              If temp(1) > 0 Then
                  For i = 2 To rg.Columns.Count
                      temp(i) = 0
                  Next
                  temp(1) = leftover
              Else
                  For i = 2 To rg.Columns.Count
                      If temp(i) > 0 Then temp(i) = leftover Else temp(i) = 0
                  Next
              End If
          Else
              ' Changed the below i=2 to i=1
              For i = 1 To rg.Columns.Count
                  If temp(i) < 0 Then temp(i) = leftover Else temp(i) = 0
              Next
          End If
    Towards the bottom I changed the 2 to a 1 because it sounded good and, voila, it fixed my problem. That being said, I have no idea if it was a proper fix (ie, it may have broken something else)...but it appears to work so I'm going to go with it for now .

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1