PC Review


Reply
Thread Tools Rate Thread

Compare and add

 
 
=?Utf-8?B?QWFyb24=?=
Guest
Posts: n/a
 
      15th Nov 2006
I need some help on coding the following
I have File1 and File2. I need File1 to look and Column A on File2 and if
there is Values there that is not in File1 Column A add it to the bottom of
File1 ColumnA
Example:

File1
Item Values
1234
2345
23456

File2
Item Values
123
2345
23456
231
234

It would add 231 and 234 to File1..
I have the following code that I can not get to work. Can you help?

Sub ABC()
Dim sh1 as Worksheet, sh2 as Worksheet
Dim lastrow as Long, i as Long
Dim rng as Range, cell as Range
set sh1 = Workbooks("File1.xls").Worksheets(1)
set sh2 = Workbooks("File2.xls").Worksheets(1)
lastrow = sh1.Cells(rows.count,1).End(xlup).row
i = 1
set rng = sh2.Range(sh2.cells(1,1),sh2.cells(1,1).End(xldown))
for each cell in rng
if application.countif(sh1.Columns(1),cell) = 0 then
sh1.cells(lastrow + i,1) = cell
i = i + 1
end if
Next
End sub



Thanks,
Aaron
 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      15th Nov 2006
That code ran perfectly for me and did exactly what you described you wanted
to do.

I copied the code you posted and put it in a workbook. Copied your data as
you show putting it in File1.xls and File2.xls in the first worksheet in each
in column A.. Ran the code and it produced

1234
2345
23456
231
234

in File1.xls. Exactly as you said you wanted.

I did it twice, once with the header Item Values in A1 and once with the
data starting in A1 with no header. Each worked perfectly.

I suspect you have reversed file1 and file2 with respect to the code.

--
Regards,
Tom Ogilvy




"Aaron" wrote:

> I need some help on coding the following
> I have File1 and File2. I need File1 to look and Column A on File2 and if
> there is Values there that is not in File1 Column A add it to the bottom of
> File1 ColumnA
> Example:
>
> File1
> Item Values
> 1234
> 2345
> 23456
>
> File2
> Item Values
> 123
> 2345
> 23456
> 231
> 234
>
> It would add 231 and 234 to File1..
> I have the following code that I can not get to work. Can you help?
>
> Sub ABC()
> Dim sh1 as Worksheet, sh2 as Worksheet
> Dim lastrow as Long, i as Long
> Dim rng as Range, cell as Range
> set sh1 = Workbooks("File1.xls").Worksheets(1)
> set sh2 = Workbooks("File2.xls").Worksheets(1)
> lastrow = sh1.Cells(rows.count,1).End(xlup).row
> i = 1
> set rng = sh2.Range(sh2.cells(1,1),sh2.cells(1,1).End(xldown))
> for each cell in rng
> if application.countif(sh1.Columns(1),cell) = 0 then
> sh1.cells(lastrow + i,1) = cell
> i = i + 1
> end if
> Next
> End sub
>
>
>
> Thanks,
> Aaron

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare sheet utility that can compare formula too Adam2046 Microsoft Excel Discussion 3 27th Apr 2010 09:26 AM
How do I compare cells and if FALSE compare to next cell in EXCEL =?Utf-8?B?Q2luZGll?= Microsoft Excel Worksheet Functions 0 24th Mar 2006 05:29 PM
String compare doesn't compare? Ken Soenen Microsoft Excel Programming 1 16th Jan 2006 03:40 PM
compare data from one column with another and compare result to yet another Matt Williamson Microsoft Excel Programming 1 25th Sep 2003 08:54 PM
compare data from one column with another and compare result to yet another Matt Williamson Microsoft Excel Worksheet Functions 1 25th Sep 2003 08:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:02 AM.