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
[TABLE="width: 939"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Ronsberger Nigeria Ltd.[/TD]
[TD][/TD]
[TD]Issued by NHIS[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD="colspan: 3"]OY/0250 Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State[/TD]
[/TR]
[TR]
[TD]*02792267* SANUSI[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]0 Principal SANUSI[/TD]
[TD]TEMILADE[/TD]
[TD]02/11/1980 M E/240[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1 Spouse SANUSI[/TD]
[TD]OLUWADAMILOLA 19/03/1992 F E/240[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]*02792269* OKOLO[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]0 Principal OKOLO[/TD]
[TD]EMMANUEL 23/10/1980 M E/240[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2 Child1 OKOLO[/TD]
[TD]CHUKWUEMEKA 13/06/2015 M E/240[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3 Child2 OKOLO[/TD]
[TD]UZOAMAKA 03/05/2013 F E/240[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]*02792277* AWOLOLA[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]0 Principal AWOLOLA[/TD]
[TD]AJIBOLA[/TD]
[TD]10/08/1977 M E/240[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1 Spouse AWOLOLA[/TD]
[TD]ABIDEMI[/TD]
[TD]05/08/1982 F E/240[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2 Child1 AWOLOLA[/TD]
[TD]MORAYOOLUWA 20/06/2012 F E/240[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]*02792285* AGBABIAJE[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]0 Principal AGBABIAJE[/TD]
[TD]TOLULOPE[/TD]
[TD]26/07/1985 M E/240[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]*02792353* ODUGBEMI[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]0 Principal ODUGBEMI[/TD]
[TD]RAFIU[/TD]
[TD]13/02/1981 M E/240[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1 Spouse ODUGBEMI[/TD]
[TD]TOYIN[/TD]
[TD]04/08/1990 F E/240[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2 Child1 ODUGBEMI[/TD]
[TD]OLAMIDE[/TD]
[TD]25/07/2012 F E/240[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3 Child2 ODUGBEMI[/TD]
[TD]EMMANUEL 10/10/2006 M E/240[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]*02839471* ADEJIMI[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]0 Principal ADEJIMI[/TD]
[TD]ADEKUNLE 28/06/1978 M E/344[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1 Spouse ADEJIMI[/TD]
[TD]OLUBUNMI 14/07/1979 F E/344[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2 Child1 ADEJIMI[/TD]
[TD]OLUWADABIRA 08/07/2007 M E/344[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]




Sample Expected Output

[TABLE="width: 1609"]
<tbody>[TR]
[TD]SN (Serial Number - Autogenerated)[/TD]
[TD]CLIENT_NUMBER (HEADNUMBER without the '*' + UNIT NUMBER AS IN DOCUMENT) - PLEASE TAKE NOTE OF LEADING ZEROS[/TD]
[TD]SURNAME (FAMILY NAME AS IN DOCCUMENT)[/TD]
[TD]FIRST_NAME (AS IN DOCUMENT)[/TD]
[TD]DATE_OF_BIRTH (AS IN DOCUMENT)[/TD]
[TD]GENDA (AS IN DOCUMENT)[/TD]
[TD]FAMILY_MEMBERSHIP (AS INDICATED IN DOCUMENT) - Options are: Principal, Spouse and Child[/TD]
[TD]HOSPITAL_NAME (COPY HEADER HOSPITAL NAME FOR ALL CLIENTS FOUND UNDER IT)[/TD]
[TD]HOSPITAL_ID (COPY CORRESPONDING HEADER HOSPITAL ID FOR ALL CLIENTS FOUND UNDER IT)[/TD]
[/TR]
[TR]
[TD="colspan: 6"]See Examples Below[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD]027922670[/TD]
[TD]SANUSI[/TD]
[TD]TEMILADE[/TD]
[TD]02/11/1980[/TD]
[TD]M[/TD]
[TD]Principal[/TD]
[TD]Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State[/TD]
[TD]OY/0250[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD]027922671[/TD]
[TD]SANUSI[/TD]
[TD]OLUWADAMILOLA[/TD]
[TD]19/03/1992[/TD]
[TD]F[/TD]
[TD]Spouse[/TD]
[TD]Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State[/TD]
[TD]OY/0250[/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[TD]027922690[/TD]
[TD]OKOLO[/TD]
[TD]EMMANUEL[/TD]
[TD]23/10/1980[/TD]
[TD]M[/TD]
[TD]Principal[/TD]
[TD]Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State[/TD]
[TD]OY/0250[/TD]
[/TR]
[TR]
[TD="align: right"]4[/TD]
[TD]027922691[/TD]
[TD]OKOLO[/TD]
[TD]CHUKWUEMEKA[/TD]
[TD]13/06/2015[/TD]
[TD]M[/TD]
[TD]Child[/TD]
[TD]Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State[/TD]
[TD]OY/0250[/TD]
[/TR]
[TR]
[TD="align: right"]5[/TD]
[TD]027922692[/TD]
[TD]OKOLO[/TD]
[TD]UZOAMAKA[/TD]
[TD]03/05/2013[/TD]
[TD]F[/TD]
[TD]Child[/TD]
[TD]Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State[/TD]
[TD]OY/0250[/TD]
[/TR]
[TR]
[TD="align: right"]6[/TD]
[TD]027922770[/TD]
[TD]AWOLOLA[/TD]
[TD]AJIBOLA[/TD]
[TD]10/08/1977[/TD]
[TD]M[/TD]
[TD]Principal[/TD]
[TD]Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State[/TD]
[TD]OY/0250[/TD]
[/TR]
[TR]
[TD="align: right"]7[/TD]
[TD]027922771[/TD]
[TD]AWOLOLA[/TD]
[TD]ABIDEMI[/TD]
[TD]05/08/1982[/TD]
[TD]F[/TD]
[TD]Spouse[/TD]
[TD]Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State[/TD]
[TD]OY/0250[/TD]
[/TR]
[TR]
[TD="align: right"]8[/TD]
[TD]027922772[/TD]
[TD]AWOLOLA[/TD]
[TD]MORAYOOLUWA[/TD]
[TD]20/06/2012[/TD]
[TD]F[/TD]
[TD]Child[/TD]
[TD]Catholic Hospital, Oluyoro - Oluyoro, Ibadan, Oyo State[/TD]
[TD]OY/0250[/TD]
[/TR]
</tbody>[/TABLE]

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:
I think you need to supply some details of what your trying to achieve.!!
Its not very clear.
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I think you need to supply some details of what your trying to achieve.!!
Its not very clear.

I Thank you again Mick. Im sorry I didnt reply sooner. I have been on the road. What I want to achieve is exactly the arrangement you have succeeded in helping out with.

Unfortunately, if you look at the initial table uploaded earlier, it was just a small segment from the whole document hence your solution did the magic based on the static address of the first entry (hospital Number and Hospital name).

I tried to run it through the rest of the document which I sent through Box dot com but I could not get the loop right since I was unable to properly identify the cell addresses through a loop.

I want to iterate through the entire list and be able to do exactly what was achieved through your solution. That is my challenge.

Thank you for your help all along.
 
Upvote 0
Both of the sheets you sent are of similar format , which of the sheets do you want me to use, "Sheet1" or "Input Format" ????

Sheet1 has no Hospital Names/Headers until row 1446 and different data up to that point and sheet "Input format" has not got a Hospital Name/Header in the first row. How would you like that to be resolved ????
 
Last edited:
Upvote 0
Sheet to use is Input Format

Sheet 1 is the begining of the document. What I always do is to remove all the preables and statistics (that appear on sheet1) from the top, then commence work on the main part of it. Please delete the first set of records at the begining of Input format so that the row Carrying Hospital Name appears top.

Once I get this I can try to adapt it to subsequent reports.

Thank you.
 
Upvote 0
Try this for results on sheet2.
NB:- Some of your data on sheet "Input Forum" is not in the correct columns and need adjusting !!!!
NB:- I have removed the first batch of data (No Hospital address) and cell "A1" now contains the first appearance in column "A" of the Words:- "Ronsberger Nigeria Ltd"
[TABLE="width: 143"]
<tbody>[TR]
[TD="class: xl63, width: 190, bgcolor: transparent"].
[/TD]
[/TR]
</tbody>[/TABLE]


Code:
[COLOR=navy]Sub[/COLOR] MG15Sep13
'[COLOR=green][B]Forum Code:-[/B][/COLOR]
[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] Variant, c [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Num [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Dim[/COLOR] Ray [COLOR=navy]As[/COLOR] Variant, nSp [COLOR=navy]As[/COLOR] Variant, Q [COLOR=navy]As[/COLOR] Variant, n [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] p [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Dt [COLOR=navy]As[/COLOR] Date, Aph [COLOR=navy]As[/COLOR] [COLOR=navy]String,[/COLOR] Aphn [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
[COLOR=navy]Dim[/COLOR] nStr [COLOR=navy]As[/COLOR] [COLOR=navy]String,[/COLOR] G [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
Ray = Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, 3)
ReDim nRay(1 To UBound(Ray, 1) + 1, 1 To 9)
 nRay(1, 1) = "Ser Num": nRay(1, 2) = "CLIENT_NUMBER": nRay(1, 3) = "Surname": nRay(1, 4) = "First Name": nRay(1, 5) = "DATE_OF_BIRTH": nRay(1, 6) = "GENDA": nRay(1, 7) = "FAMILY_MEMBERSHIP": nRay(1, 8) = "HOSPITAL_NAME": nRay(1, 9) = "HOSPITAL_ID"
c = 1
[COLOR=navy]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR=navy]For[/COLOR] n = 1 To UBound(Ray, 1)
   [COLOR=navy]If[/COLOR] Ray(n, 1) = "Ronsberger Nigeria Ltd." [COLOR=navy]Then[/COLOR]
           [COLOR=navy]Select[/COLOR] [COLOR=navy]Case[/COLOR] True
               [COLOR=navy]Case[/COLOR] Not IsEmpty(Ray(n + 1, 1)): Sp = Split(Ray(n + 1, 1), " "): Hd1 = Sp(0): Hd2 = Mid(Ray(n + 1, 1), Len(Sp(0)) + 2)
               [COLOR=navy]Case[/COLOR] Not IsEmpty(Ray(n + 1, 2)): Sp = Split(Ray(n + 1, 2), " "): Hd1 = Sp(0): Hd2 = Mid(Ray(n + 1, 2), Len(Sp(0)) + 2)
               [COLOR=navy]Case[/COLOR] Not IsEmpty(Ray(n + 1, 3)): Sp = Split(Ray(n + 1, 3), " "): Hd1 = Sp(0): Hd2 = Mid(Ray(n + 1, 3), Len(Sp(0)) + 2)
            [COLOR=navy]End[/COLOR] Select
   [COLOR=navy]End[/COLOR] If
    [COLOR=navy]If[/COLOR] Left(Ray(n, 1), 1) = "*" Or IsNumeric(Left(Ray(n, 1), 1)) [COLOR=navy]Then[/COLOR]
            Sp = Split(Ray(n, 1), " ")
        [COLOR=navy]If[/COLOR] Not .Exists(Sp(UBound(Sp)) & "_" & Hd1) [COLOR=navy]Then[/COLOR]
            .Add Sp(UBound(Sp)) & "_" & Hd1, Array(n, Hd1, Hd2)
        [COLOR=navy]Else[/COLOR]
            Q = .Item(Sp(UBound(Sp)) & "_" & Hd1)
                Q(0) = Q(0) & ", " & n
            .Item(Sp(UBound(Sp)) & "_" & Hd1) = Q
        [COLOR=navy]End[/COLOR] If
    [COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] n
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] K [COLOR=navy]In[/COLOR] .keys
        Dt = "1/1/1900": Aph = "": nStr = ""
        Sp = Split(.Item(K)(0), ", ")
          [COLOR=navy]For[/COLOR] n = 0 To UBound(Sp)
            [COLOR=navy]If[/COLOR] InStr(Ray(Sp(n), 1), "*") <> 0 [COLOR=navy]Then[/COLOR]
                Num = Replace(Split(Ray(Sp(n), 1), " ")(0), "*", "")
            [COLOR=navy]Else[/COLOR]
               
               nSp = Split(Trim(Ray(Sp(n), 2) & " " & Ray(Sp(n), 3)), " ")
               [COLOR=navy]If[/COLOR] UBound(nSp) > 0 [COLOR=navy]Then[/COLOR]
                    [COLOR=navy]If[/COLOR] IsDate(nSp(0)) [COLOR=navy]Then[/COLOR]
                        Dt = nSp(0)
                        Aph = nSp(1)
                    [COLOR=navy]ElseIf[/COLOR] IsDate(nSp(1)) [COLOR=navy]Then[/COLOR]
                        nStr = nSp(0)
                        Dt = nSp(1)
                        Aph = nSp(2)
                    [COLOR=navy]End[/COLOR] If
                    c = c + 1
                    nRay(c, 1) = c - 1
                    nRay(c, 2) = Num & Val(Split(Ray(Sp(n), 1), " ")(0))
                    nRay(c, 3) = Split(K, "_")(0)
                    nRay(c, 4) = nStr
                    nRay(c, 5) = CDate(Dt)
                    nRay(c, 6) = Aph
                    nRay(c, 7) = Split(Ray(Sp(n), 1), " ")(1) 
                    nRay(c, 8) = .Item(K)(2)
                 nRay(c, 9) = .Item(K)(1)
            [COLOR=navy]End[/COLOR] If
         [COLOR=navy]End[/COLOR] If
        [COLOR=navy]Next[/COLOR] n
[COLOR=navy]Next[/COLOR] K
[COLOR=navy]End[/COLOR] With
[COLOR=navy]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, 9)
    .Value = nRay
    .Columns.AutoFit
    .Borders.Weight = 2
[COLOR=navy]End[/COLOR] [COLOR=navy]With[/COLOR]
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Thank you very much Mick. I am really greatful for this noble endeavour. It worked just as I needed it.
And to all MrExcel team. Thank you too.

KokoroAyo.
 
Upvote 0
Hi Mick,

I write to sincerely express my gratitude to you for taking the pain to help me out with the solution to my age long problem.

With this solution I can now deliver my routine responsibility within time line.

Now I am looking at porting the solution to a small VB6 application with a simple user interface so that even when I am not around, someone can just run the application over the new list and get the result.

Please does this forum have a donation portal? I think its worth someone donating something so that other persons can enjoy the kind of support I have just enjoyed.

If yes, please let me have the details so I can make a token donation.

Mick, if you have any project that requires donation, can I also have it sent to me by PM?

Thank you once again.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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