Compare two excel sheets

痞子三分冷 提交于 2019-12-18 03:49:30

问题


How do I compare two excel sheet and determine which column is missing?

(I 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.


回答1:


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



回答2:


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.




回答3:


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.




回答4:


I found, that in some versions of excel is feature for comparing files - but commonly is disabled, however instaled. And one day I found by chance how use it.

If you have instaled TortoiseSVN (really): - select excel files to compare in your file manager - open context menu, then select TortoiseSVN > Diff - it opens excel in "comparing mode"



来源:https://stackoverflow.com/questions/3051610/compare-two-excel-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!