+ Reply to Thread
Results 1 to 5 of 5

Search, Copy and Paste

Hybrid View

  1. #1
    Registered User
    Join Date
    02-26-2004
    Location
    philippines
    Posts
    73

    Search, Copy and Paste

    Guys,

    Can anyone help me create a macro that do search, copy and paste? I tried searching for an existing one but there's no macro that matches to what I need. Basically, the macro needs to look in sheet2 column B, the data that it needs to look for is in sheet1 column a, once it finds the data in sheet2 column B, the original search criteria needs to be paste starting in sheet3 A1 while all the found data needs to be paste in column B. It will then proceed to look for the next data in sheet1 column once it's done searching on the first item. I can't even think how to start with this one, it's too complicated for me. I attached a sample data. The search data is in sheet2 and the lookup data is in sheet1.
    Attached Files Attached Files
    Last edited by japorms; 11-21-2008 at 10:49 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello japorms,

    How do you want to handle duplicate data?

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    02-26-2004
    Location
    philippines
    Posts
    73
    Quote Originally Posted by Leith Ross View Post
    Hello japorms,

    How do you want to handle duplicate data?

    Sincerely,
    Leith Ross
    it can be deleted.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello japorms,

    Sorry for the delay, my computer decided some of my software needed to updated. I added a button on Sheet3 that will perform the copy paste. Only the unique entries on Sheet1 are copied to Sheet3. A search is done on Sheet2 for each of these entries. When a match is found, the data from column "B" of the search result is transferred to column "B" of Sheet3 in the same row of that search term. This macro has already been added.
    Sub Macro1()
    
      Dim DstWks As Worksheet
      Dim I As Long
      Dim LastRow As Long
      Dim R As Long
      Dim Rng As Range
      
        Set DstWks = Worksheets("Sheet3")
        
       'Clear previously stored data on the destination worksheet
        DstWks.UsedRange.Clear
        
       'Copy only unique values to the destination worksheet
        With Worksheets("Sheet1")
          LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
          Set Rng = .Range(.Cells(1, "A"), .Cells(LastRow, "A"))
          Rng.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
          Rng.Copy Destination:=DstWks.Range("A1")
          .ShowAllData
        End With
        
       'Get the last row that has data on the destination worksheet
        With DstWks
          LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        End With
        
       'Search Sheet2 using the entries from the destination worksheet
        With Worksheets("Sheet2")
          For R = 1 To LastRow
            Set Results = .Cells.Find(What:=DstWks.Cells(R, 1), _
                                      After:=.Cells(1, 1), _
                                      LookIn:=xlValues, _
                                      LookAt:=xlWhole, _
                                      SearchOrder:=xlByRows, _
                                      searchDirection:=xlNext, _
                                      MatchCase:=False)
           'If a match if found, copy the contents of column "B" of Sheet2 and
           'paste it into column "B" of the destination worksheet
            If Not Results Is Nothing Then DstWks.Cells(R, 2) = Results.Offset(0, 1)
          Next R
        End With
        
    End Sub
    Sincerely,
    Leith Ross
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    02-26-2004
    Location
    philippines
    Posts
    73
    Thanks for the code but it didn't really answer my needs. I decided to use access to combine two data from excel. Thanks!

+ 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