Link to home
Start Free TrialLog in
Avatar of cheryl23
cheryl23Flag for United States of America

asked on

Excel formula needed

I hope to find an excel expert who can help create a formula, which would
Take the beginning score and subtract it from an ending score on each account number (there will be several scores for each acct #, but we just need the difference from the first score and the ending score and have that number placed in a cell.  We currently do this manually, each month.

I've attached a sample worksheet.
If you can help, it would be so appreciated.
score-sheet.xlsx
Avatar of Brad Rubin
Brad Rubin
Flag of United States of America image

I would do this with Array formulas to find Min and Max and take the difference between the two.

Create a new column with unique account numbers that will be used to reference your list of data.
Create a Max column
Create a Min column
Create a difference column

Using your example:

Find the MAX (Array formula needs to use Ctrl+Shift+Enter to set formula)
=MAX(IF($A$2:$A$46=H2,$C$2:$C$46))

Find the MIN (Array formula needs to use Ctrl+Shift+Enter to set formula)
=MIN(IF($A$2:$A$46=H2,$C$2:$C$46))

Then =Max - Min gives you the score difference...it is all automated based on your data set.

I attached your example data with the solution I noted above.
example.xlsx
Here is a VBA script that will do this for you.

It assumes that accounts are grouped but the dates can be in any order

I have used constants to make editing it for another workbook easier

Sub scores()
    
    Const ACCT_COL   As String = "A"
    Const SCORE_COL  As String = "C"
    Const DATE_COL   As String = "E"
    Const RESULT_COL As String = "F"
    Const START_ROW  As Long = 2
    
    Dim acct As String
    Dim lastRow As Long, i As Long, startRow As Long, endRow As Long
    
    lastRow = Range(ACCT_COL & Rows.Count).End(xlUp).Row
    acct = Range(ACCT_COL & START_ROW).Value
    startRow = START_ROW
    endRow = START_ROW
    
    For i = START_ROW + 1 To lastRow
        If Range(ACCT_COL & i).Value <> acct Then
            Range(RESULT_COL & i - 1).Value = Range(SCORE_COL & startRow).Value - Range(SCORE_COL & endRow).Value
            startRow = i
            endRow = i
            acct = Range(ACCT_COL & i).Value
        End If
        
        If Range(DATE_COL & i).Value < Range(DATE_COL & startRow).Value Then
            startRow = i
        End If
        
        If Range(DATE_COL & i).Value > Range(DATE_COL & endRow).Value Then
            endRow = i
        End If
    Next
    
End Sub

Open in new window

Avatar of Flyster
See attached. It uses this array formula:

=INDIRECT("C"&MATCH(A2,A:A,0))-INDIRECT("C"&MAX(ROW($2:$46)*($A$2:$A$46=A2)))

The Match function finds the row number of the first occurrence of the acct# and Max function find the last. Indirect is used to find the score associated with those row numbers. Conditional formatting is used to "white out" the duplicate values.

Flyster
score-sheet.xlsx
@Cheryl

How many unique acct# values will you have?
If you can add columns, you can add one column to check if score in row is maximum of accounts score, and one column to check if minimum.
Then you can more easily continue calculating difference.

If you are using Excel 2010 or later, i will suggest you to use Calculated Tables as in sheet2 of attached file. With this way, your formulas are more secure and easier to understand.
score-sheet.xlsx
For those of you looking at Max & Min scores, the question was relating to First and Last score. Looking at the first acct# the first and last are not the same as Max and Min.

You could however use the Max and Min on the dates.

@cheryl23 - are the scores cumulative or is each entry the score for that date? For acct# 121300 are you expecting result of:
1) -15  8 on 20 May less 23 on 20 Jan
2) 86  109 cumulative to 20 May less 23 on 20 Jan
3) Something else?

Thanks
Rob H
In my comment, I write "max" and "min" by mistake.
The sheet i made takes first and last scores by date, not by row number.
And then calculates difference as Latest Score - First Score.
Avatar of cheryl23

ASKER

Wow, thanks so much for the responses.  I really appreciate your time.
@Aikimark - Your question:  How many unique acct# values will you have?  For the 6 month period we are reviewing there are 361 unique acct#s
Rob Henson: Your question:  are the scores cumulative or is each entry the score for that date?   The scores are not cumulative.  1) 15  8 on 20 May less 23 on 20 Jan   (this is what we are looking for  POSITIVE 15,  23 minus 8)

Background:  We give an assessment and take the total score for that date.  We give assessments throughout the treatment period.  We look at first score and compare to last score (most recent date).  If score has decreased, improvement has been made during treatment (a lower INDIVIDUAL ASSESSMENT SCORE is better).  In the example above 23-8 = 15  would show a 65% improvement.

I need to review & test  the solutions submitted, may take a couple days.  Thanks to all.
@Cheryl

Thanks.  One follow-up question...
The worksheet had some empty columns (B and D).  Does this sample represent what your actual worksheet looks like?
@Cheryl

Will the data always be in chronological order? If so, the formula I provided will work for you. (See attached above)
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice one aikimark! Simple and easy.
It is certainly simpler than my first pass and second pass.  Glad you liked it.  The first time I used this type of formula was for an EE question that needed to create sequence/ID numbers within same-valued cell ranges.
http:Q_27768088.html#a38133521

I pondered and ADO solution, but didn't try it since the OP asked for a formula solution.  An example of an ADO solution is here if you're interested: http:Q_27515061.html#a37369496
Are you wanting to add an entry against each person or would you be interested in a separate report whereby you enter/select the patient acct# and the report shows you the required info for that person?

As a separate report you could make use of DMIN and DMAX functions.

Let me know if you are interested in that approach and I will work a sample for you.

Thanks
Rob H
Went for it anyway!!

See attached, update acct# at top left of Report sheet, other fields will change accordingly.

Acct# can be changed to be drop down for selection if so required.

Date fields use DMAX and DMIN based on Acct#, Score fields then use Max & Min Dates and DGET for date and acct# to get the score.

Thanks
Rob H
score-sheet.xlsx
This solution worked out great and was very easy to incorporate.  Thank you all for your help.
A couple of minor points about aikimark's (rather nice) solution:

Since the original request was to "Take the beginning score and subtract it from an ending score on each account number...", I think this:
    =IF(A3<>A2,F1-C2,"")
should have been this:
    =IF(A3<>A2,C2-F1,"")

And if there is any possibility of having only 1 score for an acct #, then both of the above will give incorrect results, as the result should technically be 0, since X - X always = 0.  I think this would fix that:
    =IF(A3<>A2,C2-F2,"")
I know the original request said "there will be several scores for each acct #", but it's hard to be sure whether cheryl was trying to imply that there will never be only 1.  What say you, Cheryl?  Either way, I think the last option above should work.
@tel2

Although it was revised by the OP, the original requirement was
Take the beginning score and subtract it from an ending score
So, the score associated with the earliest date is subtracted from the score associated with the latest date.
True, aikimark, and column F contains the earliest dates, right?  So F needs to be subtracted from C, which is C-F, right?  So why are you subtracting C from F here?:
    =IF(A3<>A2,F1-C2,"")
The earliest score is carried down to the point where the account number changes.
I'm well aware of that, aikimark, that's why I said:
  "...and column F contains the [insert: 'score from the'] earliest dates, right?".
If you can explain what is wrong with the logic of my last post, we might get to the bottom of this.  Maybe I'm missing something.
Thanks.
There is a definitional interpretation between "first" and "last" and earliest and latest.  That is all.  Your version of the formula is probably what the OP used.  I didn't ask, since the OP didn't post a comment about my formula results or how to use the results.  I do not think there is any "bottom" to get to.