+ Reply to Thread
Results 1 to 4 of 4

Copy pasting columns based on headers

  1. #1
    Registered User
    Join Date
    10-02-2012
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010
    Posts
    13

    Copy pasting columns based on headers

    Hello attached is an excel sheet with a macro to copy paste columns having specific heading. will someone help me concise the macro. Right now its a huge page of codes. I am new to vba and learning by recording and then modifying.
    Attached Files Attached Files

  2. #2
    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: Copy pasting columns based on headers

    Hi, krash297,

    insetad of switching sheets set object to the sheets. And instead of having multiple codes for each item use an array which holds the values and loop through that array:
    Please Login or Register  to view this content.
    Ciao,
    Holger
    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

  3. #3
    Registered User
    Join Date
    10-02-2012
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Copy pasting columns based on headers

    i am new to vba i dont know how to use arrays could you explain me how this works so that i can relate it.

  4. #4
    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: Copy pasting columns based on headers

    Hi, krash297,

    the array stores the names of the items you want to search. In the code it is filled with the line
    Please Login or Register  to view this content.
    Any item should be packed in quotes (it it is a string) and be separated by comma. This type of array is zero based which means that the first item will have the number of 0.

    Next we do have a loop which will go from the lower bound of the array (as I just stated this will be 0) to the number of items being stored - 1. Instead of writing numbers we use the command LBound (lower bound) and UBound (upper bound) which will free us to alter the code if the number of the items to search for is changed.

    Ciao,
    Holger

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Copy and Pasting Data in Columns Based on Criteria
    By forexcellence13 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2013, 07:47 PM
  2. Summary - Copy the columns based on column headers
    By rafiomeon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-01-2012, 10:32 AM
  3. Excel 2007 : Macro to copy columns based headers
    By rls1316 in forum Excel General
    Replies: 2
    Last Post: 04-25-2011, 10:22 PM
  4. Macro to Sum Columns based on Headers
    By Jluc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-24-2011, 02:36 PM
  5. Macro for comparing headers and copy/pasting respective columns
    By Sarushka in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-04-2009, 08:48 AM

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