+ Reply to Thread
Results 1 to 4 of 4

Copy pasting columns based on headers

Hybrid View

krash297 Copy pasting columns based on... 07-14-2013, 12:38 PM
HaHoBe Re: Copy pasting columns... 07-14-2013, 01:01 PM
krash297 Re: Copy pasting columns... 07-14-2013, 01:39 PM
HaHoBe Re: Copy pasting columns... 07-14-2013, 02:13 PM
  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: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    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:
    Sub EF938993()
    Dim arrFinds
    Dim lngArray As Long
    Dim wsSign As Worksheet
    Dim wsSgnot As Worksheet
    Dim rngFound As Range
    Dim lngCol As Long
    Dim lngLast As Long
    
    arrFinds = Array("Project", "Activity", "Activity and Work Area", "WA Stage", "Status", _
            "QIL Error Rate", "QQ Error Rate", "Start Date WA", "End Date WA")
        
    Set wsSign = Sheets("Signout")
    Set wsSgnot = Sheets("Sgnotproces")
    
    For lngArray = LBound(arrFinds) To UBound(arrFinds)
      Set rngFound = wsSign.Cells.Find(arrFinds(lngArray), , , , xlByRows, xlNext, True, , False)
      If Not rngFound Is Nothing Then
        lngLast = wsSign.Cells(Rows.Count, rngFound.Column).End(xlUp).Row
        lngCol = lngCol + 1
        wsSgnot.Range(wsSgnot.Cells(1, lngCol), wsSgnot.Cells(lngLast, lngCol)).Value = _
            wsSign.Range(wsSign.Cells(1, rngFound.Column), wsSign.Cells(lngLast, rngFound.Column)).Value
      End If
    Next lngArray
    wsSign.UsedRange.ClearContents
    
    Set wsSgnot = Nothing
    Set wsSign = Nothing
    End Sub
    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: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    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
    arrFinds = Array("Project", "Activity", "Activity and Work Area", "WA Stage", "Status", _
            "QIL Error Rate", "QQ Error Rate", "Start Date WA", "End Date WA")
    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