+ Reply to Thread
Results 1 to 9 of 9

How to Create Macro to Search, Copy, & Paste?

Hybrid View

hariexcel1987 How to Create Macro to... 10-30-2014, 09:49 AM
Olly Re: How to Create Macro to... 10-30-2014, 10:02 AM
hariexcel1987 Re: How to Create Macro to... 10-30-2014, 10:07 AM
Olly Re: How to Create Macro to... 10-30-2014, 10:21 AM
hariexcel1987 Re: How to Create Macro to... 10-30-2014, 10:43 AM
hariexcel1987 Re: How to Create Macro to... 11-04-2014, 02:56 AM
Olly Re: How to Create Macro to... 10-30-2014, 11:05 AM
Olly Re: How to Create Macro to... 11-04-2014, 04:55 AM
hariexcel1987 Re: How to Create Macro to... 11-04-2014, 08:22 AM
  1. #1
    Registered User
    Join Date
    11-17-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    38

    Question How to Create Macro to Search, Copy, & Paste?

    I have Two Excel sheets Sheet2: Database file(Row wise Data has been updated i.e only in A column- refer below

    HTML Code: 
    Sheet1: MIS Report (with the heading of the columns).

    In Sheet1 -i have populated the headings like Name , Company, city etc.


    HTML Code: 

    Macro Requirement :

    So here,i wanted to create a macro to search for all the same heading names and copy the next row data and paste into under respective headings in Sheet 1

    Please do let me know if you have any questions on the same and Thanks a ton in Advance.

    Example references :

    Sheet 2 : Data (A) - looks like as below


    Name
    Arun
    Company
    XXXX
    Title
    Business Team





    City
    LONDON
    Telephone


    State
    LONDON
    Mobile


    Country
    United Kingdom
    Email



    Area of responsibility
    Sales
    Business focus
    Securities
    Name Vijay Company XXXX Title Business Team

    City
    LONDON
    Telephone


    State
    LONDON
    Mobile


    Country
    United Kingdom
    Email



    Area of responsibility
    Sales
    Business focus
    Securities

    1. Like this i have 20000 rows in a A column , here now i wanted to search in a "Sheet2" with the heading name of Sheet1 (Ex :Name,Company )
    copy the next row data and paste into sheet1 under respective headings

    3.we should do Find NEXT until we copied all the data
    Here is the Macro code that i have recorded


    Sheets("Sheet2").Select
     Range("A1").Select
        Selection.Copy
        Sheets("Sheet1").Select
        Cells.Find(What:="Name", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
            xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
        Range("A37").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet2").Select
        Range("A2").Select
        ActiveSheet.Paste
    
    Sheets("Sheet1").Select
    Application.CutCopyMode = False
    Cells.Find(What:="Name", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
        xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    Range("A71").Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range("A3").Select
    ActiveSheet.Paste

    Note: There are few blanks rows in between(Sheet1)

    I have no idea how to create a macro however i have searched all over the internet to find one that i could modify to insert my own data, and this is what ive come up with.

    Please help.
    Last edited by hariexcel1987; 10-30-2014 at 09:54 AM.

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: How to Create Macro to Search, Copy, & Paste?

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    11-17-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: How to Create Macro to Search, Copy, & Paste?

    Herewith enclosed is the sample excel file, kindly help.
    Attached Files Attached Files

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: How to Create Macro to Search, Copy, & Paste?

    Try this:
    Sub foo()
        Dim lRow As Long, lEndRow As Long, lPasteRow As Long, lPasteCol As Long
        Dim wsSrc As Worksheet, wsTgt As Worksheet, c As Range, v As Variant
    
        Set wsSrc = Worksheets("Sheet2")
        Set wsTgt = Worksheets("Sheet1")
    
        lPasteRow = wsTgt.Cells(Rows.Count, 1).End(xlUp).Row
        With wsSrc
            lEndRow = .Cells(Rows.Count, 1).End(xlUp).Row
            For lRow = 1 To lEndRow
                If .Cells(lRow, 1).Value = "Name" Then lPasteRow = lPasteRow + 1
                v = .Cells(lRow, 1).Value
                Set c = wsTgt.Range("1:1").Find(v)
                If Not c Is Nothing And Not v = "" Then
                    wsTgt.Cells(lPasteRow, c.Column).Value = .Cells(lRow + 1, 1).Value
                End If
            Next lRow
        End With
    End Sub

  5. #5
    Registered User
    Join Date
    11-17-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: How to Create Macro to Search, Copy, & Paste?

    Wow, Fantastic mind blowing & unbelievable .It's working fine.


    Thanks a ton, Thanks Thanks Thanks

  6. #6
    Registered User
    Join Date
    11-17-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: How to Create Macro to Search, Copy, & Paste?

    Hi ,


    I am getting few technical errors, especially company name is not copied properly .

    Herewith attached are the examples, can you please fix this.

    Regards,
    Hari
    Attached Files Attached Files

  7. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: How to Create Macro to Search, Copy, & Paste?

    Happy to help.

    Don't forget to mark the thread solved, and add reputation to helpful posts.

  8. #8
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: How to Create Macro to Search, Copy, & Paste?

    Ah yes, it's including partial matches, so it matches "NY" with "CompaNY" :D

    Easy to fix - change this line:
    Set c = wsTgt.Range("1:1").Find(what:=v, lookat:=xlWhole)

  9. #9
    Registered User
    Join Date
    11-17-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: How to Create Macro to Search, Copy, & Paste?

    Wow, you are really a Genius.

    I will get this thread as solved. Many thanks.

+ 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] Vba/Macro Search Rows for value and copy/paste accordingly
    By Aquabat in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-21-2013, 02:56 PM
  2. [SOLVED] Search, copy & paste Macro
    By Keixm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-01-2012, 12:11 PM
  3. Search, Copy Paste Macro
    By crazyAMP in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 11-17-2011, 04:47 PM
  4. vba macro - search ID, copy, paste
    By sgp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2011, 07:00 AM
  5. Search, copy and paste macro.
    By Shin278 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-16-2011, 07:42 AM

Tags for this Thread

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