You are here:

Excel/VBA to find and copy the row based on data in column

Advertisement


Question
QUESTION: Hi Stuart
I need a VBA code to copy row based on data in column.
The data in column A consists of First name, I have data in the  column B of sheet2 which contains first name as well as last name & other details. I need a vba code which will loop through the data in column A, and find in the sheet2, if found the whole row should be copied to Sheet3.
For example if I have "Michael" in column A of sheet1 and "Michael Porter 45646" in Column B of sheet2. The whole row which contains the data should be copied to sheet3.

Please find the recorded macro for your reference.

Sub Macro1()
   Range("A2").Select
   Selection.Copy
   Sheets("Sheet2").Select
   Cells.Find(What:="Michael", After:=ActiveCell, LookIn:=xlFormulas, _
       LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
       MatchCase:=False, SearchFormat:=False).Activate
   Rows("2:2").Select
   Range("B2").Activate
   Application.CutCopyMode = False
   Selection.Copy
   Sheets("Sheet3").Select
   Range("A2").Select
   ActiveSheet.Paste
   Application.CutCopyMode = False
End Sub

But the problem is when I try to copy the contents of A2 in sheet1 and search in sheet2, it gets hardcoded (for eg. Cells.Find(What:="Michael") .I require row where the word is found should get copied to sheet 3  . Also I require loop through the cells in sheet1 (column A) till the non blank cells.

Thank you in advance for your kind help.

Best Regards,
Sanjay

ANSWER: Sub Macro1()
   Dim wksListOfNames As Worksheet, wksCopyFrom As Worksheet
   Dim wksPasteTo As Worksheet
   Dim cellWithName As Range, nameOnSheet2 As Range
   Dim pasteHere As Range
   Set wksListOfNames = ThisWorkbook.Sheets("Sheet1")
   Set wksCopyFrom = ThisWorkbook.Sheets("Sheet2")
   Set wksPasteTo = ThisWorkbook.Sheets("Sheet3")
   Set cellWithName = wksListOfNames.Range("A2")
   Do Until cellWithName.Formula = ""
       Set nameOnSheet2 = wksCopyFrom.Range("b1").EntireColumn.Find _
         (What:=cellWithName, LookAt:=xlPart, MatchCase:=False, _
         SearchFormat:=False)
       If Not (nameOnSheet2 Is Nothing) Then
         nameOnSheet2.EntireRow.Copy
         Set pasteHere = wksPasteTo.Range("A1")
         Do Until Application.CountA(pasteHere.EntireRow) = 0
         Set pasteHere = pasteHere.Offset(1)
         Loop
         wksPasteTo.Paste pasteHere
         Application.CutCopyMode = False
       End If
       Set cellWithName = cellWithName.Offset(1)
   Loop
End Sub

---------- FOLLOW-UP ----------

QUESTION: Hi Stuart,

Thank you for the the response.

The code works fine, but the problem is only when the data is found in the first instance it gets copied. But we may have several instances.

For example if I have "Michael" in column A of sheet1 and "Michael Porter 45646" in Column B and row2 of sheet2, and also "Michael Patrick 54655" in Column B or row3, both the rows should be copied to sheet3. The whole rows which contains the data should be copied to sheet3.

Best Regards,

Sanjay Sahu

Answer
Sub Macro2()
  Dim wksListOfNames As Worksheet, wksCopyFrom As Worksheet
  Dim wksPasteTo As Worksheet
  Dim cellWithName As Range, nameOnSheet2 As Range
  Dim pasteHere As Range, lookForName As Range
  Set wksListOfNames = ThisWorkbook.Sheets("Sheet1")
  Set wksCopyFrom = ThisWorkbook.Sheets("Sheet2")
  Set wksPasteTo = ThisWorkbook.Sheets("Sheet3")
  Set cellWithName = wksListOfNames.Range("A2")
  Do Until cellWithName.Formula = ""
       Set nameOnSheet2 = wksCopyFrom.Range("b1")
       Do Until nameOnSheet2.Row > nameOnSheet2.SpecialCells(xlCellTypeLastCell).Row
         If nameOnSheet2.Value = cellWithName.Value Then
         nameOnSheet2.EntireRow.Copy
         Set pasteHere = wksPasteTo.Range("A1")
         Do Until Application.CountA(pasteHere.EntireRow) = 0
         Set pasteHere = pasteHere.Offset(1)
         Loop
         wksPasteTo.Paste pasteHere
         Application.CutCopyMode = False
         End If
         Set nameOnSheet2 = nameOnSheet2.Offset(1)
       Loop
      Set cellWithName = cellWithName.Offset(1)
  Loop
End Sub
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Stuart Resnick

Expertise

I can answer questions relating to MS Excel formulas, or to programming with vba (Visual Basic for Applications) in the Excel environment. Please follow the following guidelines: your question should focus on one specific issue you want to learn. It's beyond the scope of this free service for me to create entire projects or complex vba solutions for you from scratch. You should be able to do most of the work yourself, and come here when you need help with a specific point you're stuck on. ALWAYS include a simple, concrete example illustrating what you want to learn. Explain this example in detail in the text of your question (what data is in which cells of which sheets, etc). Be very precise about the results you want, using this sample to make the logic clear. Always keep these examples SIMPLE. Never e.g. use 18 worksheets in your example if using 2 or 3 will do. Never use ranges like AI567:BB865 if using a range like A1:B3 will do. Thanks.

Experience

As a consultant, I've designed Excel tools since the 90s, working for the Federal Reserve Bank, AT&T, and (currently) Gap Inc.

Education/Credentials
My only "education" comes from 2 decades of doing spreadsheet/programming work, with major SF Bay Area corporations such as AT&T, Federal Reserve Bank, and Gap Inc.

©2016 About.com. All rights reserved.