+ Reply to Thread
Results 1 to 23 of 23

VBA to extract values from grouped data and populate a new worksheet

  1. #1
    Registered User
    Join Date
    04-01-2019
    Location
    Scotland
    MS-Off Ver
    2007
    Posts
    15

    VBA to extract values from grouped data and populate a new worksheet

    Hi I am struggling to create the VBA code I need to perform a fairly complicated task.

    I have an initial worksheet called 'Data_KPI4' which is in the format of below:

    ##See BEFORE worksheet attached##

    So I need the VBA to perform the following steps:

    • Iterate through the worksheet and group into separate sets of rows based on 'Episode Reference'. For example the first group for EP205143 would have 7 rows from the worksheet above.
    • For each group search for the row that has 'Event Type' = 'Triage' and store the values in that row for 'Attendance Date', 'Episode Reference', 'PIN' & 'Start Date'. Ideally these will be written to a new worksheet as a new row in that worksheet.
    • Also within that same group I need to create a list of dates appending all Appointment Dates in that group to the list and also append to this list all Attendance Dates in that group (which are for Event Types not equal to Triage - ie discount any Attendance Dates for Triage events). To then sort this list from oldest to newest dates and to take the earliest date in the list and add as 'First Offered Appointment Date' it to the row in the new worksheet that was populated in the previous step.
    • To iterate through all groups creating new rows for every group in the new worksheet.
    The final output should be a new worksheet called 'FinalData_KPI4' which looks like the following:

    ##See AFTER worksheet attached##

    A bit of an ask but would be very grateful for any help with this as I am working in the health sector and this will help our data projections no end from the manual audit we currently have to do!
    Attached Files Attached Files

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA to extract values from grouped data and populate a new worksheet

    Maybe :
    Please Login or Register  to view this content.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Registered User
    Join Date
    04-01-2019
    Location
    Scotland
    MS-Off Ver
    2007
    Posts
    15

    Re: VBA to extract values from grouped data and populate a new worksheet

    Thanks for having a go.

    On running I get "Runtime error 5 - Invalid procedure call or argument" and it debugs to line j = z(a(i, 5))

    Any ideas?

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA to extract values from grouped data and populate a new worksheet

    No idea, not happening here (unless your data is not the same format as your attached file)
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    04-01-2019
    Location
    Scotland
    MS-Off Ver
    2007
    Posts
    15

    Re: VBA to extract values from grouped data and populate a new worksheet

    The original file is much larger than the test one I quoted above (it has over 700 rows) so that might be an issue.

    I also just tried downloading your file and running Macro test in Excel 2013 and I get a different error "Runtime error type 13 - type mismatch" and on debug it refers to line a(i, 2) = CDate(a(i, 2))

    Any ideas?

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA to extract values from grouped data and populate a new worksheet

    In your attached file, all the "date" columns (column B,C,G) is formatted as string, not date.
    In my system (dd/mm/yyyy), just converting the string to date using cdate() function is worked, probably you are using system that different format (mm/dd/yyyy).

    Try this code to fix the "date" problem (but it won't fix Runtime error 5) :

    Please Login or Register  to view this content.

    Number of rows should'nt be an issue, it is more likely you have different format (e.g. do you have blank row(s) or blank cell(s) ?)

  7. #7
    Registered User
    Join Date
    04-01-2019
    Location
    Scotland
    MS-Off Ver
    2007
    Posts
    15

    Re: VBA to extract values from grouped data and populate a new worksheet

    Thanks I suspect you are right about the system setting (not sure how I check that but we are in the Uk so use dd-mm-yyyy to my knowledge although the system locale may be set differently) but is there a typo in the fix line I think:

    a(i, 2) = DateSerial(CLng(Right$(a(i, 2), 4)), CLng(Mid$(a(i, 2), 4, 2)), CLng(Left$(a(i, 2), 2)))

    - as I still get error 13 and on that line now.

  8. #8
    Registered User
    Join Date
    04-01-2019
    Location
    Scotland
    MS-Off Ver
    2007
    Posts
    15

    Re: VBA to extract values from grouped data and populate a new worksheet

    Just checked and my current system locale is English (United Kingdom) which has region settings for short date as dd/MM/yyyy and for long date dd MMMM yyyy.

  9. #9
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA to extract values from grouped data and populate a new worksheet

    You are welcome.

    If the column B is stay formatted as string, the fix using :
    a(i, 2) = DateSerial(CLng(Right$(a(i, 2), 4)), CLng(Mid$(a(i, 2), 4, 2)), CLng(Left$(a(i, 2), 2)))
    should overcome the runtime 13 error problem, regardless of your locale system


    As for Runtime 5 error
    - Have you check if your data has blank rows or cells ?

    Or, the "Triage" here is very important too :
    - Does the word "Triage" on column D is different in your attached file with your actual data (possibly using other word that is not "Triage") ?
    - Is there any group that miss the "Triage" row ? For example if you delete row 2, so now for group EP205143, this group doesn't have a row that has "Triage" data.
    Last edited by karedog; 11-18-2020 at 07:36 AM.

  10. #10
    Registered User
    Join Date
    04-01-2019
    Location
    Scotland
    MS-Off Ver
    2007
    Posts
    15

    Re: VBA to extract values from grouped data and populate a new worksheet

    if I copy and paste the rows from my original table over the top of the one you saved and sent me your sort works. That is good thanks.

    Now I just need to work out how to upscale this for the main file of 700 or so rows.

    I think you are right there are 4 or 5 blank Attendance Date cells for the Triage events. I there any way to build in a way of ignoring these groups entirely and not taking over a row for them to the new worksheet?

    I think all rows should have a Triage event.

  11. #11
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA to extract values from grouped data and populate a new worksheet

    Ok, then now you can try this modified code :
    Please Login or Register  to view this content.

  12. #12
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    467

    Re: VBA to extract values from grouped data and populate a new worksheet

    Hi

    karedog's method is efficient.

    I tried a different approach, using a helper-column and advanced-filter-extract to get the First Offered Appointment Date.
    The assumption is that the First Offered Appointment Date is the one immediately following the Triage record.
    I have added comments to the vba code to help you follow the method I used.
    To get the First Appointent records, click the button on the sheet [FinalData_KPI4]

    zeddy
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    04-01-2019
    Location
    Scotland
    MS-Off Ver
    2007
    Posts
    15

    Re: VBA to extract values from grouped data and populate a new worksheet

    Quote Originally Posted by karedog View Post
    Ok, then now you can try this modified code :
    Please Login or Register  to view this content.
    Hi thanks we are getting there now! I tested with my full data set and I still get some incorrect values in the last column 'First Offered Appointment Date'. To illustrate this I have sent a new section of the data anonymised so you can see where the errors are. The list of appointment dates and attendance dates for non Triage events is not returning the earliest date from that list for some reason every time. I have uploaded a new before and after file below. The last column should never really be blank looking at the data and some of the dates are not what I would expect.

    Edit: changed attachment as hadn't fully anonymised it please use current copy.
    Attached Files Attached Files
    Last edited by 83dons; 11-18-2020 at 11:32 AM.

  14. #14
    Registered User
    Join Date
    04-01-2019
    Location
    Scotland
    MS-Off Ver
    2007
    Posts
    15

    Re: VBA to extract values from grouped data and populate a new worksheet

    Quote Originally Posted by zzzeddy View Post
    Hi

    karedog's method is efficient.

    I tried a different approach, using a helper-column and advanced-filter-extract to get the First Offered Appointment Date.
    The assumption is that the First Offered Appointment Date is the one immediately following the Triage record.
    I have added comments to the vba code to help you follow the method I used.
    To get the First Appointent records, click the button on the sheet [FinalData_KPI4]

    zeddy
    Hi I tried this for my example2 file below and it doesn't seem to run. Can you get it to run and provide the expected output as per the example2 file?

    The assumption is that the First Offered Appointment Date is the one immediately following the Triage record - I am not sure this is correct as it needs to be the earliest date from a list of appointment dates and attended dates (not including attended date for Triage event) in that group. I am not sure if it will always be in date order in these 2 columns safer to create a list of the possible dates then select the earliest one.

  15. #15
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA to extract values from grouped data and populate a new worksheet

    For your newest workbook, the "Attendance Date" (column B) now is in correct format (date, not string), so the code will be modified to :
    Please Login or Register  to view this content.
    And also, it seemed that you also take the value from "Appointment Date" (column C) for the calculation.
    For example, on cell AFTER!E5, you write the result as "13/05/2019", while this date is taken from BEFORE!C20 (column C, not column B).
    So, is the calculation (as you said earlier "to sort") is based earliest date on column B only, or based on both columns (B and C) ?

  16. #16
    Registered User
    Join Date
    04-01-2019
    Location
    Scotland
    MS-Off Ver
    2007
    Posts
    15

    Re: VBA to extract values from grouped data and populate a new worksheet

    Post deleted not sure how to remove!
    Last edited by 83dons; 11-18-2020 at 12:22 PM.

  17. #17
    Registered User
    Join Date
    04-01-2019
    Location
    Scotland
    MS-Off Ver
    2007
    Posts
    15

    Re: VBA to extract values from grouped data and populate a new worksheet

    Quote Originally Posted by 83dons View Post
    • Also within that same group I need to create a list of dates appending all Appointment Dates in that group to the list and also append to this list all Attendance Dates in that group (which are for Event Types not equal to Triage - ie discount any Attendance Dates for Triage events). To then sort this list from oldest to newest dates and to take the earliest date in the list and add as 'First Offered Appointment Date' it to the row in the new worksheet that was populated in the previous step.
    Hi, as per my original post. All Appointment Dates within that group and all Attendance Dates within that group (minus the Attendance Date for Triage event). From that list of dates from both places I need to return the earliest date listed. I now its a little complicated but hope that is clear?

  18. #18
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA to extract values from grouped data and populate a new worksheet

    No, it is not complicated, really. It is just the string to date problem, the unexpected blank cells, and 'to sort' (which usually performed on single column or single row) that is misleading.

    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    04-01-2019
    Location
    Scotland
    MS-Off Ver
    2007
    Posts
    15

    Re: VBA to extract values from grouped data and populate a new worksheet

    Thanks very much for all your help here. It might be useful (for me and others) if you had time sometime to add some comments to the code as I would be interested to know what each part does to better understand how it works.

    I will do a final manual spot check in the morning using the full data sheet and the new module but it certainly seems to have worked perfectly. Thanks again.

  20. #20
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA to extract values from grouped data and populate a new worksheet

    You are welcome, and here is the explanation :

    Please Login or Register  to view this content.

  21. #21
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    467

    Re: VBA to extract values from grouped data and populate a new worksheet

    Hi

    The array method from karedog is an efficient method.

    It doesn't hurt to have a second method. It allows you to check to see if you get the same results.

    The attached file gets the First Offered Appointment date that follows a Triage, for each Episode Reference.
    Any Episode Reference that does not have a corresponding Triage event will be excluded from the Final Data.

    The First Offered Appointment will be the earliest date from the Attendance/Appointment date-columns for records that match that particular Episode Reference, following the Triage event.

    If there is a Triage event which does not have any follow-up records for that associated Episode Reference, then the First Offered Appointment date will be returned as a blank i.e. no date is given.

    The VBA code in this attached example uses Filter-Copy to extract the Triage record-data to the Final Data worksheet.
    Temporary formulas are then placed on the worksheet to find the earliest-date for each data-record.
    This uses Excel's function MIN(Attendance Date, Appointment Date).
    Since a blank date cell would be considered the 'earliest', we deal with this by replacing blank-dates with a dummy-date value far into the future e.g. 31-Dec-2100.
    After processing, these dummy-date values are put back as blanks.

    In the Final Data worksheet, the data-source-row is given for each of the reported First Offered Appointment Dates.
    This allows spot-checks to be taken to confirm the source of that reported date.

    The VBA code is written to allow you to step-through (using the [F8] key) to follow the process.
    To test with your real data, just paste your records on sheet [BEFORE] etc etc etc

    zeddy
    Attached Files Attached Files
    Last edited by zzzeddy; 11-19-2020 at 11:25 AM.

  22. #22
    Registered User
    Join Date
    04-01-2019
    Location
    Scotland
    MS-Off Ver
    2007
    Posts
    15

    Re: VBA to extract values from grouped data and populate a new worksheet

    Thanks I will try this new method as well to see if I get the same results.

    Also thanks to karedog for providing the really helpful comments to the code. I can now visualise better how it works. I have never used the collection object before.

  23. #23
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: VBA to extract values from grouped data and populate a new worksheet

    Cross-posted at: https://www.mrexcel.com/board/thread...sheet.1151388/
    Please read Excel Forum's Cross-Posting policy in rule 8: http://www.excelforum.com/forum-rule...rum-rules.html
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How to extract records and populate results in the next worksheet?
    By Rev12 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-26-2018, 12:19 AM
  2. [SOLVED] Extract Data from Grouped Cells
    By Patnaik in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-13-2017, 01:55 AM
  3. [SOLVED] Select and Extract a line of Data in a Table to Populate a Template Worksheet
    By DGAlamo in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-20-2016, 11:30 AM
  4. Replies: 2
    Last Post: 04-13-2015, 08:29 AM
  5. Using Vba to extract data from one cell with multiple values into another worksheet
    By superboy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-10-2014, 02:09 AM
  6. [SOLVED] Extract best 3 values from grouped data
    By Tongki in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 02-24-2014, 03:02 AM

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