views:

75

answers:

3

how do you compare two excel sheet and determine which column is missing?

(would like to compare a list of countries from sheet A with sheet B, then mark which country is missing)

Note: they are in random order.

A: 

You can use the VLOOKUP function in an Excel worksheet to help finding "missing" data in a different sheet. For example, take the following two worksheets:

Sheet1
------
       A          B         C
1     aa 
2     bb
3     cc 
4     dd

.

Sheet2
------
       A          B         C
1     aa 
2     bb
3     dd 

Add the following formula to cell B1 in Sheet and drag the formula down through cell B4:

=IF(ISERROR(VLOOKUP(A1,Sheet2!$A$1:$A$3,1,FALSE)),"MISSING FROM OTHER SHEET","")

Sheet1 should indicate items that are missing from the other sheet in column B, like so:

Sheet1
------
       A          B                        C
1     aa 
2     bb
3     cc         MISSING FROM OTHER SHEET
4     dd
Ben McCormack
A: 

You can use ADO with Excel

Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer

''This is not the best way to refer to the workbook
''you want, but it is very conveient for notes
''It is probably best to use the name of the workbook.

strFile = ActiveWorkbook.FullName

''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used. 
''This is the Jet 4 connection string, you can get more
''here : http://www.connectionstrings.com/excel

strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

''Late binding, so no reference is needed

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")


cn.Open strCon

 ''Query example:
 strSQL = "SELECT Country " _
       & "FROM [Sheet1$] a " _
       & "WHERE Country NOT IN " _
       & "SELECT Country FROM [Sheet2$]"


''Open the recordset for more processing
''Cursor Type: 3, adOpenStatic
''Lock Type: 3, adLockOptimistic
''Not everything can be done with every cirsor type and 
''lock type. See http://www.w3schools.com/ado/met_rs_open.asp

rs.Open strSQL, cn, 3, 3

If rs.Count>0 Then
    MsgBox rs.GetString
End If

It is also possible to quickly write the recordset to a sheet with CopyFromRecordset.

Remou
A: 

The solution varies depending on the number of rows involved and the number of times you need to do this, and how you want the information to be presented.

If you don't have a lot of countries and you need to do this only once, the fastest solution is:

  • Copy both columns into a temporary sheet.
  • Sort both columns alphabetically.
  • Manually go through them and spot the differences.

If you need to do this just once, but there are lots of countries, the vlookup option is the fastest one.

If you need to repeat this procedure lots of times, and you need use that list somewhere (i.e. in other sheet) then you can use a more convoluted solution, involving two additional columns with lookups, and pivot tables. But at that point I'd look at moving it to something more manageable, like a small database.

egarcia