Macro to duplicate values/rows based on cell value

Important Notice


Please note that on 14th December 2023 users will experience an expected outage whilst we make upgrades to our network. We anticipate this process may take a couple of hours and so we apologise in advance for any inconvenience.

  • Good morning,


    I hope someone can help me - I have scoured the net for a macro that results in the below - I have come across many different threads with this sort of stuff but not quite what I want.


    Input table


    [TABLE="width: 500"]

    [tr]


    [td][/td]


    [td]

    A

    [/td]


    [td]

    B

    [/td]


    [/tr]


    [tr]


    [td]

    1

    [/td]


    [td]

    Manpower Required

    [/td]


    [td]

    QTY Required

    [/td]


    [/tr]


    [tr]


    [td]

    2

    [/td]


    [td]

    Supervisor

    [/td]


    [td]

    2

    [/td]


    [/tr]


    [tr]


    [td]

    3

    [/td]


    [td]

    Cleaner

    [/td]


    [td]

    5

    [/td]


    [/tr]


    [/TABLE]


    Output table - (Desired outcome)


    [TABLE="width: 500"]

    [tr]


    [td]

    Manpower

    [/td]


    [td]

    NO

    [/td]


    [td]

    Name

    [/td]


    [/tr]


    [tr]


    [td]

    Supervisor

    [/td]


    [td]

    1

    [/td]


    [td][/td]


    [/tr]


    [tr]


    [td]

    Supervisor

    [/td]


    [td]

    2

    [/td]


    [td][/td]


    [/tr]


    [tr]


    [td]

    Cleaner

    [/td]


    [td]

    1

    [/td]


    [td][/td]


    [/tr]


    [tr]


    [td]

    Cleaner

    [/td]


    [td]

    2

    [/td]


    [td][/td]


    [/tr]


    [tr]


    [td]

    Cleaner

    [/td]


    [td]

    3

    [/td]


    [td][/td]


    [/tr]


    [tr]


    [td]

    Cleaner

    [/td]


    [td]

    4

    [/td]


    [td][/td]


    [/tr]


    [tr]


    [td]

    Cleaner

    [/td]


    [td]

    5

    [/td]


    [td][/td]


    [/tr]


    [/TABLE]


    Basically in the output table the rows are duplicated and the number increases incrementally based on the qty - I then simply have to type in the name of the particular person.


    The input table could contain anything from 1 line of manpower entries to up to 20 entries.


    Hope I am making sense and I hope someone can help me?


    I am currently using Excel 2010.


    Thanks

  • Re: Macro to duplicate values/rows based on cell value


    Hi BaraaKhalil - thanks a lot for your reply, I have opened the document and the tables are correct however there is no macro to run (unless I am missing something?).
    Again, appreciate your time.
    Thanks

  • Re: Macro to duplicate values/rows based on cell value


    In the attachment the solution is by array formulas .. Have a look at the formulas
    If you need macro,then try this code


    Regards

  • Re: Macro to duplicate values/rows based on cell value


    Apologies BaraaKhalil I never noticed the formulas I just set off looking for a macro. Anyway, a big thank you both solutions work a treat.
    Really appreciate your expertise.
    Thank you.

  • Re: Macro to duplicate values/rows based on cell value


    BaraaKhalil, I hope you can help me further as I seem to have hit a slight problem.


    Is there a way either the formula or macro can be a bit more dynamic - for example I have attached the document with an additional column (Name) on the output table in order to allocate a person.


    The problem is though if I was to add an additional supervisor (make the supervisor 3 in the input table) the output table adds an additional supervisor pushing the 2 cleaners down but leaving the names static. So on the attached currently if you make the supervisor required 3 it leaves Jim in there but as supervisor 3 when he needs to stay as Cleaner 1. Does this make sense? can you help further?


    Thanks a lot, and sorry if I am making your head hurt.forum.ozgrid.com/index.php?attachment/72681/


    ****Edited with file attached****

  • Re: Macro to duplicate values/rows based on cell value


    I will try my best to clarify below......


    Currently
    Input Table


    [TABLE="width: 500"]

    [tr]


    [td]

    Manpower

    [/td]


    [td]

    Qty Required

    [/td]


    [/tr]


    [tr]


    [td]

    Supervisors

    [/td]


    [td]

    2

    [/td]


    [/tr]


    [tr]


    [td]

    Cleaner

    [/td]


    [td]

    2

    [/td]


    [/tr]


    [/TABLE]


    Output Table (Desired Outcome - which I already have using the original formula/macro)


    [TABLE="width: 500"]

    [tr]


    [td]

    Manpower

    [/td]


    [td]

    No

    [/td]


    [td]

    Name

    [/td]


    [/tr]


    [tr]


    [td]

    Supervisor

    [/td]


    [td]

    1

    [/td]


    [td]

    Bill

    [/td]


    [/tr]


    [tr]


    [td]

    Supervisor

    [/td]


    [td]

    2

    [/td]


    [td]

    Ben

    [/td]


    [/tr]


    [tr]


    [td]

    Cleaner

    [/td]


    [td]

    1

    [/td]


    [td]

    Jim

    [/td]


    [/tr]


    [tr]


    [td]

    Cleaner

    [/td]


    [td]

    2

    [/td]


    [td]

    Bob

    [/td]


    [/tr]


    [/TABLE]


    Scenario......
    Say I now want to add another Supervisor in the input table to make it 3 supervisors....


    Input table
    [TABLE="width: 500"]

    [tr]


    [td]

    Manpower

    [/td]


    [td]

    Qty Required

    [/td]


    [/tr]


    [tr]


    [td]

    Supervisors

    [/td]


    [td]

    3

    [/td]


    [/tr]


    [tr]


    [td]

    Cleaner

    [/td]


    [td]

    2

    [/td]


    [/tr]


    [/TABLE]


    Desired Output
    [TABLE="width: 500"]

    [tr]


    [td]

    Manpower

    [/td]


    [td]

    No

    [/td]


    [td]

    Name

    [/td]


    [/tr]


    [tr]


    [td]

    Supervisor

    [/td]


    [td]

    1

    [/td]


    [td]

    Bill

    [/td]


    [/tr]


    [tr]


    [td]

    Supervisor

    [/td]


    [td]

    2

    [/td]


    [td]

    Ben

    [/td]


    [/tr]


    [tr]


    [td]

    Supervisor

    [/td]


    [td]

    3

    [/td]


    [td][/td]


    [/tr]


    [tr]


    [td]

    Cleaner

    [/td]


    [td]

    1

    [/td]


    [td]

    Jim

    [/td]


    [/tr]


    [tr]


    [td]

    Cleaner

    [/td]


    [td]

    2

    [/td]


    [td]

    Bob

    [/td]


    [/tr]


    [/TABLE]


    So now I have added an extra supervisor I want the output table to insert 'Supervisor 3' in the right place but move the cleaner rows down so that the names I have allocated move down aswell. In the current version if I add an extra supervisor Jim becomes supervisor 3 and Bob is cleaner 1 with cleaner 2 empty.


    I hope this make sense?


    Thanks a lot

  • Re: Macro to duplicate values/rows based on cell value


    Hello try this code (It is not for me of course)

  • Re: Macro to duplicate values/rows based on cell value


    BaraaKhalil thats cracked it - exactly what I was after - thank you very much for the time taken to help on this.

  • Re: Macro to duplicate values/rows based on cell value


    BaraaKhalil,


    I have had to insert 3 rows above the table headers so the input table starts at A4 (as opposed to A1) and the output table starts at E4 (as opposed to E1). Subsequently the macro no longer works.


    What part of the code needs tweaking to mitigate these extra rows?


    Thanks again.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!