Hello. I'm fairly new with VBA. I have data that is more of a vertical format, but I need to move it to a new worksheet, and put all of the data for one patient on a single row. Please look at the following data example to understand my explanation.
Each new patient record always starts with MSH in the A column. After than is a row with an error, followed by the patient data. After that, there may or may not be one or more rows with OBR in column A. There may or may not be a row with IN1 in column A. And if Column A contains 'critical error', then the last row for that patient will always be a duplicate of the error line that immediately following the MSH row. The value in column A is always one of the following (MSH, CRITICAL ERROR or ERROR, PID, OBR, IN1), however the error will be followed by more text.
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]MSH[/TD]
[TD]KENNEDY[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]CRITICAL ERROR: unable to match patient[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]PID[/TD]
[TD]BUNNY, BUGS[/TD]
[TD]DOB[/TD]
[TD]M[/TD]
[TD]ADDRESS[/TD]
[TD]PHONE[/TD]
[/TR]
[TR]
[TD]CRITICAL ERROR: unable to match patient[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]MSH[/TD]
[TD]KENNEDY[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]ERROR: invalid data[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]PID[/TD]
[TD]BUNNY, LOLA[/TD]
[TD]DOB[/TD]
[TD]F[/TD]
[TD]ADDRESS[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]OBR[/TD]
[TD]ABD US[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]MSH[/TD]
[TD]INTERGY-RIS[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]ERROR: missing provider[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]PID[/TD]
[TD]DEVIL, TAZ[/TD]
[TD]DOB[/TD]
[TD]M[/TD]
[TD][/TD]
[TD]PHONE[/TD]
[/TR]
[TR]
[TD]IN1[/TD]
[TD]UNITED HEALTH[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]MSH[/TD]
[TD]LABCORP[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]ERROR: invalid data[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]PID[/TD]
[TD]LEGHORN, FOGHORN[/TD]
[TD]DOB[/TD]
[TD]M[/TD]
[TD]ADDRESS[/TD]
[TD]PHONE[/TD]
[/TR]
[TR]
[TD]OBR[/TD]
[TD]LYME DISEASE[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]OBR[/TD]
[TD]STREP[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
I need for this data to look like the following example, on it's own spreadsheet within the same workbook.
There can be multiple rows per patient for the OBRs, but only one of them needs to be copied. I used the last one, since my thought was just to keep copying them, and the last one would be the one that would stick. If the error is a critical error, the last row before the next patient will always be a duplicate of the row that is right after the MSH, so again, it can be copied over with no harm.
[TABLE="class: grid, width: 750"]
<tbody>[TR]
[TD]BUNNY, BUGS[/TD]
[TD]DOB[/TD]
[TD]M[/TD]
[TD]ADDRESS[/TD]
[TD]PHONE[/TD]
[TD][/TD]
[TD][/TD]
[TD]KENNEDY[/TD]
[TD]CRITICAL ERROR[/TD]
[/TR]
[TR]
[TD]BUNNY, LOLA[/TD]
[TD]DOB[/TD]
[TD]F[/TD]
[TD]ADDRESS[/TD]
[TD][/TD]
[TD][/TD]
[TD]ABD US[/TD]
[TD]KENNEDY[/TD]
[TD]ERROR: invalid data[/TD]
[/TR]
[TR]
[TD]DEVIL, TAZ[/TD]
[TD]DOB[/TD]
[TD]M[/TD]
[TD][/TD]
[TD]PHONE[/TD]
[TD]UNITED HEALTH[/TD]
[TD][/TD]
[TD]INTERGY-RIS[/TD]
[TD]ERROR: missing provider[/TD]
[/TR]
[TR]
[TD]LEGHORN, FOGHORN[/TD]
[TD]DOB[/TD]
[TD]M[/TD]
[TD]ADDRESS[/TD]
[TD]PHONE[/TD]
[TD][/TD]
[TD]STREP[/TD]
[TD]LABCORP[/TD]
[TD]ERROR: invalid data[/TD]
[/TR]
</tbody>[/TABLE]
Any help would be greatly appreciated, since I have no idea how to make this work, especially with the variable number of rows for each patient.
(Note: If anyone is familiar with HL7 messages, I'm converting HL7 messages to an Excel format. I'm willing to share everything else I've done if you're interested.)
Thank you very much,
Robin Tardif
Each new patient record always starts with MSH in the A column. After than is a row with an error, followed by the patient data. After that, there may or may not be one or more rows with OBR in column A. There may or may not be a row with IN1 in column A. And if Column A contains 'critical error', then the last row for that patient will always be a duplicate of the error line that immediately following the MSH row. The value in column A is always one of the following (MSH, CRITICAL ERROR or ERROR, PID, OBR, IN1), however the error will be followed by more text.
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]MSH[/TD]
[TD]KENNEDY[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]CRITICAL ERROR: unable to match patient[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]PID[/TD]
[TD]BUNNY, BUGS[/TD]
[TD]DOB[/TD]
[TD]M[/TD]
[TD]ADDRESS[/TD]
[TD]PHONE[/TD]
[/TR]
[TR]
[TD]CRITICAL ERROR: unable to match patient[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]MSH[/TD]
[TD]KENNEDY[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]ERROR: invalid data[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]PID[/TD]
[TD]BUNNY, LOLA[/TD]
[TD]DOB[/TD]
[TD]F[/TD]
[TD]ADDRESS[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]OBR[/TD]
[TD]ABD US[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]MSH[/TD]
[TD]INTERGY-RIS[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]ERROR: missing provider[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]PID[/TD]
[TD]DEVIL, TAZ[/TD]
[TD]DOB[/TD]
[TD]M[/TD]
[TD][/TD]
[TD]PHONE[/TD]
[/TR]
[TR]
[TD]IN1[/TD]
[TD]UNITED HEALTH[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]MSH[/TD]
[TD]LABCORP[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]ERROR: invalid data[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]PID[/TD]
[TD]LEGHORN, FOGHORN[/TD]
[TD]DOB[/TD]
[TD]M[/TD]
[TD]ADDRESS[/TD]
[TD]PHONE[/TD]
[/TR]
[TR]
[TD]OBR[/TD]
[TD]LYME DISEASE[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]OBR[/TD]
[TD]STREP[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
I need for this data to look like the following example, on it's own spreadsheet within the same workbook.
There can be multiple rows per patient for the OBRs, but only one of them needs to be copied. I used the last one, since my thought was just to keep copying them, and the last one would be the one that would stick. If the error is a critical error, the last row before the next patient will always be a duplicate of the row that is right after the MSH, so again, it can be copied over with no harm.
[TABLE="class: grid, width: 750"]
<tbody>[TR]
[TD]BUNNY, BUGS[/TD]
[TD]DOB[/TD]
[TD]M[/TD]
[TD]ADDRESS[/TD]
[TD]PHONE[/TD]
[TD][/TD]
[TD][/TD]
[TD]KENNEDY[/TD]
[TD]CRITICAL ERROR[/TD]
[/TR]
[TR]
[TD]BUNNY, LOLA[/TD]
[TD]DOB[/TD]
[TD]F[/TD]
[TD]ADDRESS[/TD]
[TD][/TD]
[TD][/TD]
[TD]ABD US[/TD]
[TD]KENNEDY[/TD]
[TD]ERROR: invalid data[/TD]
[/TR]
[TR]
[TD]DEVIL, TAZ[/TD]
[TD]DOB[/TD]
[TD]M[/TD]
[TD][/TD]
[TD]PHONE[/TD]
[TD]UNITED HEALTH[/TD]
[TD][/TD]
[TD]INTERGY-RIS[/TD]
[TD]ERROR: missing provider[/TD]
[/TR]
[TR]
[TD]LEGHORN, FOGHORN[/TD]
[TD]DOB[/TD]
[TD]M[/TD]
[TD]ADDRESS[/TD]
[TD]PHONE[/TD]
[TD][/TD]
[TD]STREP[/TD]
[TD]LABCORP[/TD]
[TD]ERROR: invalid data[/TD]
[/TR]
</tbody>[/TABLE]
Any help would be greatly appreciated, since I have no idea how to make this work, especially with the variable number of rows for each patient.
(Note: If anyone is familiar with HL7 messages, I'm converting HL7 messages to an Excel format. I'm willing to share everything else I've done if you're interested.)
Thank you very much,
Robin Tardif