+ Reply to Thread
Results 1 to 11 of 11

Macro to copy 3 adjacent columns at a time to a separate worksheet & sort by Company name

  1. #1
    Registered User
    Join Date
    11-27-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    10

    Macro to copy 3 adjacent columns at a time to a separate worksheet & sort by Company name

    Hi all,

    I hope you guys are able to help out with this macro. I have attached a workbook, where Sheet 1 contains the raw data. (In the real example there will be a variable no. of rows/columns of data).

    'Company Code', 'RM' & 'Charge Amounts' form the set of 3 columns on the sheet that I need to copy & paste into Sheet 2. The macro should be able to copy the subsequent 3 columns also to Sheet 2 (Company Code (1) , RM (1), Charge Amounts (1)..so on. It should essentially follow a loop until there's no more columns left to be copied.

    Once all data is collected on Sheet 2, a sort needs to occur by Company Code. I think I can handle this part.

    But any help with copying in a loop will be appreciated.

    Thanks,
    Anya
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to copy 3 adjacent columns at a time to a separate worksheet & sort by Company

    Please Login or Register  to view this content.
    Last edited by patel45; 04-20-2013 at 06:30 AM.
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    11-27-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Macro to copy 3 adjacent columns at a time to a separate worksheet & sort by Company

    Hi, thanks heaps for that. That worked for the columns present in the worksheet.But there can be any no. of columns with data that needs to be pasted in Sheet 2.

    How can I account for that? Also The 3 columns on Sheet 2 should have the header row with column names i.e. Company Code, RM, Charge amount.

    It would be great if I could make it work for more columns.

    Thanks Patel45

  4. #4
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Macro to copy 3 adjacent columns at a time to a separate worksheet & sort by Company

    @ patel45,

    Good one!

    What is the Stop suppose to do?

    I got it working as follows:

    Please Login or Register  to view this content.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to copy 3 adjacent columns at a time to a separate worksheet & sort by Company

    Hi, anya1,

    Please Login or Register  to view this content.
    The code should take care of the columns to copy.

    Ciao,
    Holger
    Last edited by HaHoBe; 04-20-2013 at 06:21 AM. Reason: edited for correction in text
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  6. #6
    Registered User
    Join Date
    11-27-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Macro to copy 3 adjacent columns at a time to a separate worksheet & sort by Company

    Hi Patel45,

    I figured that the Stop was a typo in the code!

    Any chance of making it work for more columns in sets of 3,though? I almost feel guilty asking you help on this further after your prompt reply already...

    Ta

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to copy 3 adjacent columns at a time to a separate worksheet & sort by Company

    Hi, anya1,

    the Stop command halts the execution of the code and often is set as a permament break point (whereas F9 in the code is only active as long as the project is open).

    Ciao,
    Holger

  8. #8
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Macro to copy 3 adjacent columns at a time to a separate worksheet & sort by Company

    patel45 gave some good code here.

    Try the attached WorkBook, and see if it works for you now.
    Attached Files Attached Files

  9. #9
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Macro to copy 3 adjacent columns at a time to a separate worksheet & sort by Company

    Hi anya1,

    Let's go with HaHoBe's Code. It has no Column limitations.

    @HaHoBe,

    I like that Holger!
    Attached Files Attached Files
    Last edited by Winon; 04-20-2013 at 06:47 AM.

  10. #10
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to copy 3 adjacent columns at a time to a separate worksheet & sort by Company

    I'm sorry, I missed stop removing, I changed attached code

  11. #11
    Registered User
    Join Date
    11-27-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Macro to copy 3 adjacent columns at a time to a separate worksheet & sort by Company

    Thanks heaps, guys. The code works perfectly now.

    Cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1