Copy excel entries into new work sheet and rearrange the values

KokoroAyo

New Member
Joined
Sep 8, 2017
Messages
14
I need help on an assignment. Its a list of patients meant for healthcare purposes. This list is sent to us in PDF format and we just search to find any entitled patient on the list. Now we have to convert to excel and rearrange for better management. My task now is to handle the rearrangement using macro since rearranging all manualy takes weeks and we receive the list on monthly basis.

I have never done this before and I am lost as to how to handle it. Please help.

Below is the format of the list. It is arranged in Header - List format but we need it in a different format as shown in the Sample Output format.

Sample List (with Little Explanation
1. Rows like 26645 are headers (there are footers also) and will be automatically removed
2. Hospital No (First part of Row 26646) is Unique to each Hospital). I will form a new field
3. Hospital Name Will form a new field or will be ignored
4. Row 26647 carries Family ID and Family Name. Asterisks will be removed to get the Number
5. Row 26648 Shows details of Family Head (Proncipal). Unique Id will be FamilyId + 0. That is 027926670. It forms a Unique Field
6. Principal/Spouse/Child is the Membership in the Family - hence it forms a field
7. Family Name forms a Field
8. Field B is First Name
9. Field C contains Date of Birth and Gender. This will be broken down to separate fields. Last Part of the string can be ignored (it is Batch Number)

Embedded Broken Image removed

Sample from the Original Excel Sheet
Ronsberger Nigeria Ltd.Issued by NHIS
OY/0250 Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State
*02792267* SANUSI
0 Principal SANUSITEMILADE02/11/1980 M E/240
1 Spouse SANUSIOLUWADAMILOLA 19/03/1992 F E/240
*02792269* OKOLO
0 Principal OKOLOEMMANUEL 23/10/1980 M E/240
2 Child1 OKOLOCHUKWUEMEKA 13/06/2015 M E/240
3 Child2 OKOLOUZOAMAKA 03/05/2013 F E/240
*02792277* AWOLOLA
0 Principal AWOLOLAAJIBOLA10/08/1977 M E/240
1 Spouse AWOLOLAABIDEMI05/08/1982 F E/240
2 Child1 AWOLOLAMORAYOOLUWA 20/06/2012 F E/240
*02792285* AGBABIAJE
0 Principal AGBABIAJETOLULOPE26/07/1985 M E/240
*02792353* ODUGBEMI
0 Principal ODUGBEMIRAFIU13/02/1981 M E/240
1 Spouse ODUGBEMITOYIN04/08/1990 F E/240
2 Child1 ODUGBEMIOLAMIDE25/07/2012 F E/240
3 Child2 ODUGBEMIEMMANUEL 10/10/2006 M E/240
*02839471* ADEJIMI
0 Principal ADEJIMIADEKUNLE 28/06/1978 M E/344
1 Spouse ADEJIMIOLUBUNMI 14/07/1979 F E/344
2 Child1 ADEJIMIOLUWADABIRA 08/07/2007 M E/344

<tbody>
</tbody>




Sample Expected Output

SN (Serial Number - Autogenerated)CLIENT_NUMBER (HEADNUMBER without the '*' + UNIT NUMBER AS IN DOCUMENT) - PLEASE TAKE NOTE OF LEADING ZEROSSURNAME (FAMILY NAME AS IN DOCCUMENT)FIRST_NAME (AS IN DOCUMENT)DATE_OF_BIRTH (AS IN DOCUMENT)GENDA (AS IN DOCUMENT)FAMILY_MEMBERSHIP (AS INDICATED IN DOCUMENT) - Options are: Principal, Spouse and ChildHOSPITAL_NAME (COPY HEADER HOSPITAL NAME FOR ALL CLIENTS FOUND UNDER IT)HOSPITAL_ID (COPY CORRESPONDING HEADER HOSPITAL ID FOR ALL CLIENTS FOUND UNDER IT)
See Examples Below
1027922670SANUSITEMILADE02/11/1980MPrincipalCatholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo StateOY/0250
2027922671SANUSIOLUWADAMILOLA19/03/1992FSpouseCatholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo StateOY/0250
3027922690OKOLOEMMANUEL23/10/1980MPrincipalCatholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo StateOY/0250
4027922691OKOLOCHUKWUEMEKA13/06/2015MChildCatholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo StateOY/0250
5027922692OKOLOUZOAMAKA03/05/2013FChildCatholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo StateOY/0250
6027922770AWOLOLAAJIBOLA10/08/1977MPrincipalCatholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo StateOY/0250
7027922771AWOLOLAABIDEMI05/08/1982FSpouseCatholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo StateOY/0250
8027922772AWOLOLAMORAYOOLUWA20/06/2012FChildCatholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo StateOY/0250

<tbody>
</tbody>

My major problem is the rearrangement of this data.
If required, I can upload a sample workbook if there is an avenue for uploading file, or I can put it in a place and share the link.

Thanks in advance.
 
Last edited by a moderator:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Sample Expected Output (reformatted). Thank you Peter_SSs for your kind guidance.
Excel 2007
ABCDEFGHI
1SNCLIENTNOFAMILYNAMEFIRST_NAMEBIRTHDATESEXFAMILY_MEMBERSHIPHOSPITALIDHOSPITAL_NAME
21027922670SANUSITEMILADE02/11/1980MPrincipalOY/0250Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State
32027922671SANUSIOLUWADAMILOLA19/03/1992FSpouseOY/0250Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State
43027922690OKOLOEMMANUEL23/10/1980MPrincipalOY/0250Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State
54027922691OKOLOCHUKWUEMEKA13/06/2015MChildOY/0250Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State
65027922692OKOLOUZOAMAKA03/05/2013FChildOY/0250Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State
76027922770AWOLOLAAJIBOLA10/08/1977MPrincipalOY/0250Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State
87027922771AWOLOLAABIDEMI05/08/1982FSpouseOY/0250Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State
98027922772AWOLOLAMORAYOOLUWA20/06/2012FChildOY/0250Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State

<tbody>
</tbody>
OUTPUT SAMPLE

And this is the Original data (as converted from the PDF document)
Excel 2007
ABC
26644
26645Ronsberger Nigeria Ltd.Issued by NHIS
26646OY/0250 Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State
26647*02792267* SANUSI
266480 Principal SANUSITEMILADE02/11/1980 M E/240
266491 Spouse SANUSIOLUWADAMILOLA 19/03/1992 F E/240
26650*02792269* OKOLO
266510 Principal OKOLOEMMANUEL 23/10/1980 M E/240
266522 Child1 OKOLOCHUKWUEMEKA 13/06/2015 M E/240
266533 Child2 OKOLOUZOAMAKA 03/05/2013 F E/240
26654*02792277* AWOLOLA
266550 Principal AWOLOLAAJIBOLA10/08/1977 M E/240
266561 Spouse AWOLOLAABIDEMI05/08/1982 F E/240
266572 Child1 AWOLOLAMORAYOOLUWA 20/06/2012 F E/240

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Original
 
Upvote 0
Based on your original data, Try this for results on sheet2 starting "A1".
NB:- I have assumed that the Data (starting point) :- " *02792267* SANUSI" is in cell "A3".
<colgroup><col width="211" style="width: 158pt; mso-width-source: userset; mso-width-alt: 7509;"> <tbody> </tbody>

Code:
[COLOR="Navy"]Sub[/COLOR] MG10Sep17
[COLOR="Navy"]Dim[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant, Hd1 [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Hd2 [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, R [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, nSp [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Set[/COLOR] Rng = Range("A3", Range("A" & Rows.Count).End(xlUp))
ReDim ray(1 To Rng.Count + 1, 1 To 9)
 ray(1, 1) = "Ser Num": ray(1, 2) = "CLIENT_NUMBER": ray(1, 3) = "Surname": ray(1, 4) = "First Name": ray(1, 5) = "DATE_OF_BIRTH": ray(1, 6) = "GENDA": ray(1, 7) = "FAMILY_MEMBERSHIP": ray(1, 8) = "HOSPITAL_NAME": ray(1, 9) = "HOSPITAL_ID"
   Sp = Split(Range("B2"), " ")
     Hd1 = Sp(0): Hd2 = Mid(Range("B2"), Len(Sp(0)) + 2)
       c = 1
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    Sp = Split(Dn.Value, " ")
    [COLOR="Navy"]If[/COLOR] Not .Exists(Sp(UBound(Sp))) [COLOR="Navy"]Then[/COLOR]
        .Add Sp(UBound(Sp)), Dn
    [COLOR="Navy"]Else[/COLOR]
        [COLOR="Navy"]Set[/COLOR] .Item(Sp(UBound(Sp))) = Union(.Item(Sp(UBound(Sp))), Dn)
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] .Item(K)
        Sp = Split(R, " ")
            [COLOR="Navy"]If[/COLOR] InStr(R, "*") <> 0 [COLOR="Navy"]Then[/COLOR]
                Num = Replace(Sp(0), "*", "")
            [COLOR="Navy"]Else[/COLOR]
               nSp = Split(R.Offset(, 1).Value & " " & R.Offset(, 2).Value, " ")
                c = c + 1
                ray(c, 1) = c - 1
                ray(c, 2) = Num & Val(Sp(0))
                ray(c, 3) = K
                ray(c, 4) = nSp(0)
                ray(c, 5) = CDate(nSp(1))
                ray(c, 6) = nSp(2)
                ray(c, 7) = Sp(1)
                ray(c, 8) = Hd2
                ray(c, 9) = Hd1
            [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]Next[/COLOR] R
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] Sheets("Sheet9").Range("A1").Resize(c, 9)
    .Value = ray
    .Columns.AutoFit
    .Borders.Weight = 2
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Just a Note :-
Change the line below (at bottom of code) to "Sheet2" from "sheet9".
Code:
With  Sheets("Sheet2").Range("A1").Resize(c, 9)
 
Upvote 0
Thank you very much Mick for your quick response.

However, I could not get it to run as it returns "Runtime Error '9'. Subscript out of range.

And the above error refers to Line 7:
Sp = Split(Range("B2"), " ")Thank you once again.
 
Upvote 0
Upvote 0
You're welcome
If you still want me to look at your file please post file use Box.com.
 
Upvote 0

Forum statistics

Threads
1,215,357
Messages
6,124,483
Members
449,165
Latest member
ChipDude83

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top