Search : in
By :

Compare cell a1 to entire A col in sheet2

Last answer on Oct 11, 2009 6:24:13 am BST Bill, on Aug 28, 2008 5:49:35 pm BST 
 Report this message to moderators

Hello,

I have been trying to compare sheet1 A2 to sheet2 A2 through A500 and if it exists somewhere in sheet2's a col then copy that entire row to a new sheet.

I have experimented with many bits of macro code from others but so far no real success.


Figuring out how to say that in code is a bit difficult for me. I'm struggling to get the hang of this.

Maybe...

sub compare()
Dim LastRow_1 As Integer
Dim LastCol_1 As Integer
Dim Data_1 As range
Dim LastRow_2 As Integer
Dim LastCol_2 As Integer
Dim Data_2 As range

Dim Sh_1 As Worksheet
Dim Sh_2 As Worksheet

Dim X As Long
Dim Y As Long
Dim C_1 As range
Dim C_2 As range

Set Sh_1 = ActiveWorkbook.Sheets("Master")
Set Sh_2 = ActiveWorkbook.Sheets("Inventory")

LastRow_1 = Sh_1.range("A5000").End(xlUp).Row
LastCol_1 = Sh_1.range("A5000").End(xlToLeft).Column
Set Data_1 = Sh_1.range("A2").Resize(LastRow_1, LastCol_1)

LastRow_2 = Sh_2.range("A5000").End(xlUp).Row
LastCol_2 = Sh_2.range("A5000").End(xlToLeft).Column
Set Data_2 = Sh_2.range("A2").Resize(LastRow_2, LastCol_2)

For Each C_1 In Data_1
For Each C_2 In Data_2
If C_2 = C_1 Then
'found a cell on sheet2 that matched cell in sheet1
'now do what you need to do
Data_2.EntireRow.Copy Destination:=Worksheets("New_Master").range("A5000").End(xlUp).Offset(1, 0)

End If


Next C_2
Next C_1
end sub


When I run this it just copies everything to the sheet called New_Master, including the non-matches.

What am I missing? It has to be obvious, that's usually the case. ;-)

Thanks,

Bill

Configuration: Windows XP
Firefox 2.0.0.11

Best answers for « compare cell a1 to entire A col in sheet2 » in :
Excel tips : How to insert date in a cell Show Excel tips : How to insert date in a cell Below are some tips on how to insert date and time in an excel cell for a specific purpose:- To insert current date, press CTRL + ; in the chosen cell. To insert current time, press CTRL+...
Basic Excel Formulas ShowBasic Excel Formulas Below are some basic formulas for Microsoft excel: Basic formula : ADDITION cell A1 to A10 = sum (A1: A10) AVERAGE cell A1 to A10 = average (A1: A10) MAXIMUM cell A1 to A10 = max (A1: A10) MINIMUM...
Quickly create a simple holiday schedule ShowQuickly create a simple holiday schedule Here's a simple tip on how creating an Holiday schedule. A1: Enter Leaves 2008 A2: Enter Name Firstname Ax A3: Enter the names of people to handle x B1: Enter 01/05/08; Format /...
Excel - Send value of cell to target ShowExcel - Send value of cell to target Issue Solution Note Issue Is it possible to send the value of a cell (copy and paste special) to another cell where the value would not be disturbed when the source value changes to '0'? My...
Download USA Unlisted Cell Phone Numbers ShowHas you friend flown for America without telling you where he/she would settle? Dont panic! USA Unlisted Cell Phone Numbers is a program which can help you. By using this program, you simply need to know his/her phone number in order to find...
Spreadsheets - Cell Selection ShowCell Selection Spreadsheets are powerful tools for working with data. However, to work with data, it is necessary to have tools to rapidly choose the required cells. Line Selection An entire line can be chosen by clicking directly on the line...
Worksheet - Cells ShowThe Concept of a Cell A "cell" is the intersection between a line (horizontal) and a column (vertical) on a worksheet. Thus, the name of the line combined with the name of the column gives the cell's coordinates (the term address is sometimes also...
Enterprise Application Integration (EAI) ShowIntroduction to the EAI concept The purpose of EAI (Enterprise Application Integration) is the interoperability and organization of the flow of information between heterogeneous applications, i.e. ensure communcation between the different...

1

Ivan-hoe, on Sep 2, 2008 8:55:00 am BST
  • +2

Hello Bill,
when you write

Data_2.EntireRow.Copy

you copy the whole range "Data_2", which corresponds to the column A of the sheet "Inventory"
If you want to copy only the rows in Data_2 that match with Data_1, do write
C_2.EntireRow.Copy 

I hope it helps
Ivan

Reply to Ivan-hoe

2

bill, on Sep 2, 2008 12:48:04 pm BST
  • +2

Ivan,

COOL! Thanks!

Bill

Reply to bill

3

 pira, on Feb 18, 2009 5:34:07 am GMT
  • +1

Very use full macro . thank you for sharing.

Reply to pira