+ Reply to Thread
Results 1 to 5 of 5

pull data from Sheet 1 to Sheet 2

Hybrid View

  1. #1
    Registered User
    Join Date
    12-29-2013
    Location
    Hanoi
    MS-Off Ver
    Excel 2003
    Posts
    3

    pull data from Sheet 1 to Sheet 2

    I would like to pull data from Sheet 1 to Sheet 2 of same excel file. Sheet 2 is the master sheet and it has 20 columns (headers are customer information such as firstname, lastname, phone etc.). Sheet 1 has some of the headers of Sheet 2. At the start Sheet 2 is empty (only has headers), and Sheet 1 has both headers and data.

    I would like a macro that:
    - Go through all column headers of Sheet 2.
    - For each column header, copy the whole column (if exists) from Sheet 1 to corresponding column in Sheet 2.

    Can someone help please tks in advance. I was inspired by the following post, but can't rewrite that . - http://en.kioskea.net/forum/affich-2...cified-columns


    Example:
    Sheet 2:

    FirstName----LastName----Phone

    Sheet 1:
    Phone---------LastName
    bb-------------cc
    bb-------------cc
    bb-------------cc


    Desired result after executing the macro:
    Sheet 2:

    FirstName-------------Phone---------LastName
    -------------------------cc-------------bb
    -------------------------cc-------------bb
    -------------------------cc-------------bb

    Thanks all in advance!
    Last edited by thanhson; 12-29-2013 at 07:01 AM. Reason: add explanation

  2. #2
    Registered User
    Join Date
    12-29-2013
    Location
    Hanoi
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: pull data from Sheet 1 to Sheet 2

    anyone helping out? tks.

  3. #3
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: pull data from Sheet 1 to Sheet 2

    Try:
    Sub CopyCol()
        Application.ScreenUpdating = False
        Sheets("Sheet1").Activate
        Dim lCol1 As Long
        lCol1 = Sheets("Sheet1").Range("IV1").End(xlToLeft).Column
        Dim colHeader As Range
        Dim foundVal As Range
        For Each colHeader In Sheets("Sheet1").Range(Cells(1, 1), Cells(1, lCol1))
            With Sheets("Sheet2").Rows(1)
                Set foundVal = .Find(what:=colHeader, _
                After:=.Cells(.Cells.Count), _
                LookIn:=xlValues, _
                LookAt:=xlWhole, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False)
            If Not foundVal Is Nothing Then
                Columns(colHeader.Column).Copy Sheets("Sheet2").Cells(1, foundVal.Column)
            End If
            End With
        Next colHeader
        Application.ScreenUpdating = True
    End Sub
    Last edited by Mumps1; 12-29-2013 at 03:12 PM.

  4. #4
    Registered User
    Join Date
    12-29-2013
    Location
    Hanoi
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: pull data from Sheet 1 to Sheet 2

    beautiful code Mumps1. works faultlessly. big thanks to you Mumps1.

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: pull data from Sheet 1 to Sheet 2

    My pleasure.

+ 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. pull data from two sheet and paste into master sheet with desired column's only
    By breadwinner in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-13-2014, 03:39 AM
  2. Replies: 0
    Last Post: 05-21-2013, 03:09 PM
  3. Pull data from a specific sheet that matches cell value on Input Sheet
    By vaciaravino in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-12-2012, 08:38 PM
  4. Replies: 4
    Last Post: 09-05-2012, 05:57 PM
  5. Replies: 4
    Last Post: 08-29-2012, 10:32 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