Collate most common text values from multiple worksheets + more

zookcruzr

New member
Joined
Jul 17, 2014
Messages
6
Reaction score
0
Points
0
Location
Australia
Hello all,
This is a twofold Question/Post so please specify which one you are answering or querying about.

I have a spreadsheet recording details of incidents that occur in my area with individual worksheets for every month.
These incidents are broken down with the following columns;
Date
Event No
Patient Name
Offence or MH condition
Drug Related?
Alcohol Related?
Suburb
Time at Scene (Record Minutes)
Travel Time
Hospital Waiting Time
Return travel time
Total Time
Officers Involved
Sec 22 Police vehicle Escort
Sec 22 Ambulance with police Escort
Sec 20 Ambuance with police Escort
Section 20 Ambulance Escort
Other

As you can see, I'm not really in a position to post the whole spreadsheet as it contains personal information.

The last worksheet I have a formatted report to print off giving summaries of various fields for the year, broken down by the month.

Q1:
For each month I have the most common "offence or MH condition". This field HAS to be a free text field, so I cannot create a standard list for drop downs.
At the moment I am using the following array formula;
{=INDEX('Jan 14'!D:d,MATCH(MAX(COUNTIF('Jan 14'!D:d,'Jan 14'!D:d)),COUNTIF('Jan 14'!D:d,'Jan 14'!D:d),0))}
- it is D: D but it converted this to :D hence the lower case d - and yes I know it doesn't matter with case lol -

My question is, whether or not I can do this without using an array formula, as it slows down the data entry for the fields it refers to.
I am not against using a macro, BUT, I am not familiar or versed in the process.

Q2:
I'd like to collate the "top 20" patients and how many times they have been involved for that year.
The issue I am having is referencing multiple worksheets to collate this information as they are text fields, AND have to remain as such.
I currently have been playing with a pivot table set up, but am unsure how to reference multiple worksheets and limiting to the 'top 20'.
I think I could possibly create lists (200 rows to ensure total capture) of this column for each month and use those lists in a pivot table or macro, but doing so is beyond my knowledge.
What would be the best way to approach this?

NB: Because this spreadsheet is stored on a shared network drive I want to limit the total size of the file and calculations it has to do to ensure the speedy opening/saving and data entry.
It will be used by people who are not familiar with Excel let alone could be called computer literate. So I am trying to make most things autopopulate/autocalculate.


Any help you can offer would be highly appreciated, but if it is in macro form please dumb it down as I am not versed in it, as stated above.

Regards,

Levi
 

zookcruzr

New member
Joined
Jul 17, 2014
Messages
6
Reaction score
0
Points
0
Location
Australia
I think I may have solved part of Q2.
I have created a PivotTable using 'Multiple Consolidation Ranges'.
I've been able to sort the Patients by their 'count' ie how many times they appear across all worksheets.

What I am now trying to do is limit this list to the 'Top 20', however this function is not returning what I want lol.
 

Rizky

New member
Joined
Apr 28, 2014
Messages
44
Reaction score
0
Points
0
Maybe you should attach your sample workbook to attrack the solver here, and with explain the expected results too, its difficult without seeing your file, to attach click "Go Advanced" button and find Paperclip button to attach your file

Regards
 

Bob Phillips

Super Moderator
Staff member
Joined
Mar 21, 2011
Messages
1,942
Reaction score
0
Points
36
Excel Version(s)
O365
You could try creating a name range for offences to restrict the formula from using the whole column. The formula for the named range could be say named as offences with a refersto value of

='JAN 14'!$D2:INDEX('JAN 14'!$D:$D,COUNTA('JAN 14'!$D:$D),1)

It would have to be a local worksheet name not a workbook name as you would have one on every sheet. Your calculation formula would still be an array formula, but it would be

=INDEX('JAN 14'!offences,MATCH(MAX(COUNTIF('JAN 14'!offences,'JAN 14'!offences)),COUNTIF('JAN 14'!offences,'JAN 14'!offences),0))

If that doesn't speed it up, you could always set calculation mode to manual when entering data and reset back to automatic afterwards.
 

p45cal

Super Moderator
Staff member
Joined
Dec 16, 2012
Messages
2,200
Reaction score
16
Points
38
Excel Version(s)
365
I think I may have solved part of Q2.
I have created a PivotTable using 'Multiple Consolidation Ranges'.
I've been able to sort the Patients by their 'count' ie how many times they appear across all worksheets.

What I am now trying to do is limit this list to the 'Top 20', however this function is not returning what I want lol.
Yes, I have found that using multiple consolidation ranges in a pivot makes it very hard to get what you want.

What I'd do is consolidate all the source data into one table; you have the date in the leftmost column, so a pivot table can be restricted to report on any period you want.
Personally, I'd keep the source data as a single table, and ditch the monthly sheets, however, if you want you could keep the monthly sheets and have a macro consolidate the data by copying all the sheets into one table, but I'd need a sample workbook to work with, with say the sensitive data adjusted (not removed!). You could do this with find and replace.
 

zookcruzr

New member
Joined
Jul 17, 2014
Messages
6
Reaction score
0
Points
0
Location
Australia
Thank you for all your responses. Its appreciated.

I know the easiest option would be to post a sample, however to the get the results I want I would need to change the names and keep atleast 20 entries in each sheet. All while maintaining low percentage of duplicity. Too much work :) . I use Excel to limit the manual data manipulation and entry I have to do lol.

I feel the best option may be to have a macro collating all the data I want in one sheet so it can be ordered alphabetically, then use the pivot table to order by count and limit to top 20. I just can't get my head around macros yet.

Bob, that does make sense thankyou. Would that ignore blanks?

Essentially I'm trying to make it simple and usable for plebs like myself, but pretty and self calculating for the management :D

Whilst writing this I have decided to make a sample lol. I have removed the last names, suburb and internal reference numbers as they are not important for what I need to do. I have also limited the data to around 20 each month, BUT, any solutions need to accommodate for more than 20. I have limited to a max of 200. It is very unlikely ever to get over 100, but you never know.

If I need to explain what I need further, please ask, but it should be self explanatory coupled with the sheet and op.
I have left all the months so any formula or macro will still work. I have only back-captured data to April, so that is why there is none further than that.
 

Attachments

  • Copy of Mental Health Transport Database 2014 (EVESMRPT).xlsx
    201.3 KB · Views: 19

p45cal

Super Moderator
Staff member
Joined
Dec 16, 2012
Messages
2,200
Reaction score
16
Points
38
Excel Version(s)
365
I know the easiest option would be to post a sample, however to the get the results I want I would need to change the names and keep atleast 20 entries in each sheet. All while maintaining low percentage of duplicity. Too much work :) .
…and even more work for those considering trying to answer your query.

Whilst writing this I have decided to make a sample lol.
That's better!

A macro to be put in a standard code-module f the file you attached:
Code:
Sub blah()
SheetsToExclude = "SAMPLE PIVOT,Reports,Single Table,New Pivot"
Headers = Array("Date", "Event No", "Patient Name", "Offence or MH condition", "Drug Related?", "Alcohol Related? ", "Suburb", "Time at Scene (Record Minutes)", "Travel Time", "Hospital Waiting Time", "Return travel time", "Total Time", "Sec 22 Police vehicle Escort", "Sec 22 Ambulance  with police Escort", "Sec 20 Ambuance  with police Escort", "Section 20 Ambulance Escort", "Other")
Set SingleTableSheet = ThisWorkbook.Sheets.Add(after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
SingleTableSheet.Cells(1, "A").Resize(, UBound(Headers) + 1) = Headers
SingleTableSheet.Name = "Single Table"
DestRow = 2
For Each sht In ThisWorkbook.Worksheets
  If InStr(SheetsToExclude, sht.Name) = 0 Then
    With sht
      '.Select
      lr = .Cells(.Rows.Count, "A").End(xlUp).Row
      If lr > 1 Then
        'process
        .Cells(2, "A").Resize(lr - 1, 17).Copy SingleTableSheet.Cells(DestRow, 1)
        DestRow = DestRow + lr - 1
      End If
    End With
  End If
Next sht
End Sub
and an attached workbook with just a consolidated table made by the macro, then I added 2 pivot tables manually.
 

Attachments

  • ExcelGuru3267.xlsx
    30.3 KB · Views: 33

zookcruzr

New member
Joined
Jul 17, 2014
Messages
6
Reaction score
0
Points
0
Location
Australia
Thanks p45cal. I appreciate the help. I have a bad tendency to write as I think, so I apologise if I offended anyone or came across as ungrateful.

The macro works perfectly, I was hoping this would help me to have a layered (2 tiered) ordering within the Pivot Table of 'Top20 Patients', however it didn't help.

In relation to Q1 (array formula), I couldn't work out a formula to replace the array one, even with the consolidated data.
I have resorted to turning to Manual Calculation, calculating then saving the document. This is fine for me, however for someone who struggles with the simplest tasks within Excel it may be too comlicated, and they wouldn't be able to wait for the array formula to catch up (1 - 2 sec) once data is entered in the cell referenced.

I am starting to think a macro would be the best thing for both problems. I could set it up like a form (as in access), so that a manager could just press a button, have the macro run and formulate the data in a printable report. Essentially what I have done, but have a macro to automatically do it with a press of a button.

So.... Q3: Is it possible for a macro to;
a) calculate multiple formulae in different specified cells (one of which would be the array formula in question)?
b) create a specifically formatted - consolidated ranged pivot table and sort it based on a sum value range?

Thank you in advance to anyone who takes the time to answer. If clarification is needed please ask.
 

p45cal

Super Moderator
Staff member
Joined
Dec 16, 2012
Messages
2,200
Reaction score
16
Points
38
Excel Version(s)
365
Would you be happy to ditch the monthly sheets in favour of a single table?
It would:
1. Make the formulae much easier, especially if it's a real excel Table.
2. Not be prone to users putting new information on the wrong sheet.
3. Allow any further reporting tobe much easier.
Downside:
Users getting the day/month element of the date wrong (although this can be mitigated (and, in the current setup, it looks like they have to put the date in correctly anyway)).
 

zookcruzr

New member
Joined
Jul 17, 2014
Messages
6
Reaction score
0
Points
0
Location
Australia
Believe it or not, by having it in one table would complicate things.
It is the way it is due to the procedural training that has already been drummed into the officers.
I understand what you are saying, however this is the way it has to stay.
Plus that decision is not up to me, and has already been made lol
 

p45cal

Super Moderator
Staff member
Joined
Dec 16, 2012
Messages
2,200
Reaction score
16
Points
38
Excel Version(s)
365
Try this macro. It overwrites values in the Reports sheet (not the grand totals/averages yet).
It will process every sheet except those in the line:SheetsToExclude = "Reports,SAMPLE PIVOT,Single Table", so add to this list if necessary.
It renders redundant the totals at the bottom of each sheet.
It doesn't use pivot tables at all.
It creates formulae based on the ranges on each sheet which contain data; it works out what that range is by doing the equivalent of selecting the bottomost cell of column A of the entire sheet and doing a keyboard End, up-arrow, so don't have anything in column A below the table on the monthly sheets.
Because these formulae now only work on small ranges (instead of entire colums with a million rows) calculation time is negligible.
I've added a Police Involved column to those sheets which didn't have it, to make them all the same.
You'll need to tell me what data you want in the Average Staff column of the Reports sheet.
Code:
Sub blah2()
SheetsToExclude = "Reports,SAMPLE PIVOT,Single Table"
DestnRow = 4
For Each sht In ThisWorkbook.Sheets
  If InStr(1, SheetsToExclude, sht.Name, vbTextCompare) = 0 Then
    lr = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    Set myrng = sht.Range("A2:A" & lr)
    zz = "'" & sht.Name & "'!" & myrng.Address(ReferenceStyle:=xlR1C1)
    With Sheets("Reports")
      With .Cells(DestnRow, "A")
        .NumberFormat = "@"
        .Value = sht.Name
      End With
      .Cells(DestnRow, "B").FormulaR1C1 = "=COUNTA(" & zz & ")"
      zz = "'" & sht.Name & "'!" & myrng.Offset(, 3).Address(ReferenceStyle:=xlR1C1)
      .Cells(DestnRow, "C").FormulaArray = "=INDEX(" & zz & ",MATCH(MAX(COUNTIF(" & zz & "," & zz & ")),COUNTIF(" & zz & "," & zz & "),0))"
      .Cells(DestnRow, "D").FormulaR1C1 = "=SUM('" & sht.Name & "'!R2C12:R" & lr & "C12)/1440"
      .Cells(DestnRow, "E").FormulaR1C1 = "=RC[-1]*1440/RC[-3]"
      Dim Sections(1 To 5)
      For i = 1 To 5
        Sections(i) = Application.WorksheetFunction.CountA(myrng.Offset(, 12 + i))
      Next i
      .Cells(DestnRow, "G").Value = sht.Range("N1:R1").Cells(Application.Match(Application.Max(Sections), Sections, 0))
      '.Cells(DestnRow, "F").FormulaR1C1 = "=SUM((SUM('Jul 14'!C[7]))/RC[-4])"
      DestnRow = DestnRow + 1
    End With
  End If
Next sht
End Sub
 
Top