Help on VBA loop - paste on last row

  • I am fairly new to VBA and struggling to understand why my macro is not working.

    The context: I have an Excel workbook with 3 sheets:

    • Sheet"Department" with a list of names
    • Sheet"Accounts" with a list of accounts
    • Sheet"Departments and Accounts" which is where my output should be

    What I am trying to do I want to :

    • Copy the list of all the accounts from the Sheet "Accounts" and paste it on the Sheet "Departments and Accounts"in column A
    • go to the Sheet "Department", copy the first department name
    • go to the sheet "Departments and Accounts" and paste the department name in column B until next to the account
    • repeat until all my department have been pasted

    Desired output I would have the bloc with all the accounts with one department next to it, and as many blocs as there are departments on the list. On the sheet it would look like this in the picture I attached.


    My code so far:


    It is simply not working at all - it just pastes the accounts but that's it, and I don't understand why. Could you please help me fixing it ?


    Thank you!!

  • Attach your workbook and manually show what result you require

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Thanks. You have 177 Accounts and 45 Departments, so you want every account listed with every department (a total of 7,965 rows of data)?

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Thanks. You have 177 Accounts and 45 Departments, so you want every account listed with every department (a total of 7,965 rows of data)?

    Yes - I know it's a lot, but most of the files I deal with have 10k lines. What I mostly need is to understand the code to achieve it, and/or why mine isn't producing the expected result. As I am a newbie, I need that to learn :)

  • Like so?

  • Try the attached, I added a button to the Accounts sheet, click it to run the macro.


    Macro assigned to the button:

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Like so?

    It worked perfect;y!! I will do some research to better understand your code, but thank you so much!

  • Try the attached, I added a button to the Accounts sheet, click it to run the macro.


    Macro assigned to the button:

    Works perfectly too!! Thank you very much, I realize that I have so much to learn...

  • You're welcome

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

Participate now!

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