+ Reply to Thread
Results 1 to 10 of 10

Adding record above the next line based on conditions. VBA required?

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Adding record above the next line based on conditions. VBA required?

    Hi,

    Wondering if anybody can assist. I have attached the Excel file.

    The original worksheet shows the original data arrangement, and the desired output is the final output.


    The logic is....whenever the break down necessary column shows a y in the original worksheet, we need to add a row ABOVE.

    As you can see, the first row in the desired output sheet remains the same. On the added line as you can see, the start_date becomes the end_date of the previous line, and its end_date is whenever the original end_date was. On the 3rd line, the start_date and end_date is the same in this case. All three lines will have the same admission_date assigned. We get 60 for the days_counted because it's just the subtraction of start_date from end_date.

    I know it may seem redundant the way that record needs to be inserted, but it will satisfy our reporting standard.

    Anyone able to assist? Can this be done in Excel or with some VBA? The actual worksheet would have many patient records, each identified by a unique patient ID. I would like to do this for each patient. So, it has to first check if two records belong to the same patient. (i.e. it has to be all 2, or all 3, or all 4).

    Is this clear?

    Thank you
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Adding record above the next line based on conditions. VBA required?

    Are you trying to add a blank row?

    Just kidding just saw your other worksheet, this should be simple.
    Last edited by rvasquez; 05-31-2012 at 10:03 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Adding record above the next line based on conditions. VBA required?

    Nevermind I think I understand now. Below is your attached file.
    Last edited by rvasquez; 05-31-2012 at 11:12 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Adding record above the next line based on conditions. VBA required?

    Hello there,

    Attached is your original workbook updated to include a macro that provides you with the desired results you have listed on the "desired output" worksheet.

    To run it select the "original" worksheet and then hold down the alt key and press F8 to open the macro window. Select "Macro1" and then click Run.

    To see the code hold down the Alt key and press F8 and select the Macro1. The select the Step Into option. I have inserted comments to help you understand.

    Also I have inserted a formula into column F to assign y's and n's.

    Let me know if this works for you!

    Thanks!
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding record above the next line based on conditions. VBA required?

    The code worked!

    Thank you for using the comment lines(it makes understanding the code easier!)

    I see that the use of the .Offset() is useful in this situation. Is the following possible?

    I would like the days_counted be a calculation.(it's just the end_date minus the start_start)(the 60 was manually calculated by using a formula in the formula bar).

    Also, is it possible run this macro and have Excel output the dataset in a different worksheet? It's just that I don't want to lose the original.

    Thanks
    Attached Files Attached Files

  6. #6
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Adding record above the next line based on conditions. VBA required?

    That's totally possible, right now I'm working on something else for someone but once I'm finished I can do this for you. Won't take too long if someone hasn't helped you by then

  7. #7
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Adding record above the next line based on conditions. VBA required?

    Just kidding this one isn't calculating the difference correctly just one sec.
    Attached Files Attached Files
    Last edited by rvasquez; 05-31-2012 at 03:46 PM. Reason: not correct

  8. #8
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Adding record above the next line based on conditions. VBA required?

    Okay this one works!

    Thanks!
    Attached Files Attached Files

  9. #9
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding record above the next line based on conditions. VBA required?

    Hi,

    Appreciate your help.

    I am putting this one on hold for the moment, but will take a look at it once I make a breakthrough on the other request.

  10. #10
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Adding record above the next line based on conditions. VBA required?

    No problem, I'm going to unsubscribe to this thread for now then, but if you need additonal help with this you can private message me with a link to this post and I will continue to help!

    Thanks!

+ 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