Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
| Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old December 12th, 2007, 02:41 PM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default code to check values in two columns??

Here is my problem:

I want to check each cell in column A with every cell in column B and want to copy the cell to another text file if any cell value in column A does NOT exist in column B and vice versa. That might be a very simple code but since I know writing codes as much as writing in French :) this will take my days to figure it out alone.

I will appreciate any tips.

Thanks in advance.

  #2 (permalink)  
Old December 12th, 2007, 07:55 PM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

To be more specific assume that my first column A includes:

William
Joe
Tom
John
Bill

and column B includes:

Bill
William
Amy
John
Tom
Henry


I want the code to find me Joe and copy&paste it somewhere (preferably in a text file or in another sheet lets say to A1 in sheet 2) and next find Amy (and then Henry) and copy paste in another sheet (to lets say A1 and A2 in sheet 3) or in a text file.

Joe must be found because it is in column A but not in column B.
Amy&Henry must be found because they are in column B but not in column A.

Can anyone help me on this ?

  #3 (permalink)  
Old December 13th, 2007, 07:39 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: , , Finland.
Posts: 168
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
This should do the trick


Sub FindNames()
Dim Range1(), Range2()
Dim bFound As Boolean
Dim RowNo As Integer


Range1 = Range("A1:" & Range("A1").End(xlDown).Address)
Range2 = Range("B1:" & Range("B1").End(xlDown).Address)

For i = 1 To UBound(Range1)
    bFound = False
    For j = 1 To UBound(Range2)
        If Range1(i, 1) = Range2(j, 1) Then
            bFound = True
            Exit For
        End If
    Next
    If bFound = False Then
        RowNo = RowNo + 1
        Sheets(2).Cells(RowNo, 1) = Range1(i, 1)
    End If
Next
RowNo = 0
For i = 1 To UBound(Range2)
    bFound = False
    For j = 1 To UBound(Range1)
        If Range1(j, 1) = Range2(i, 1) Then
            bFound = True
            Exit For
        End If
    Next
    If bFound = False Then
        RowNo = RowNo + 1
        Sheets(3).Cells(RowNo, 1) = Range2(i, 1)
    End If
Next

End Sub

-vemaju


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy columns based on cell values EricB123 Excel VBA 0 December 18th, 2007 12:51 AM
getting values from template columns itemtemplate Laxmikant_it ASP.NET 1.0 and 1.1 Professional 4 February 6th, 2007 04:19 PM
Check values Dj Kat MySQL 2 March 23rd, 2006 04:10 AM
Displaying values in multiple columns in Combo abhijeet BOOK: Professional C#, 2nd and 3rd Editions 1 June 15th, 2005 07:01 AM
how can i check for duplicate values before saving noor ASP.NET 1.0 and 1.1 Basics 3 June 10th, 2005 09:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.