用VBS比較兩個Excel檔案的資料
relevantcodes.com的一篇文章《VBScript: Compare 2 Excel Files》中介紹瞭如何用VBScript來比較兩個Excel檔案的資料:
http://relevantcodes.com/vbscript-compare-2-excel-files/
主要是使用了Excel的COM介面的range物件來實現的。支援比較資料並且高亮顯示差異:
Class clsComparer
'[--- Region Private Variables Start ---]
Private oExcel 'Excel.Application
Private arrRangeUno 'Range.Value (array) of the Primary Excel spreadsheet
Private arrRangeDos 'Range.Value (array) of the Secondary Excecl spreadsheet
Private oDict 'Scripting.Dictionary containing unmatched cells
'[--- Region Private Variables End ---]
'[--- Region Public Variables Start ---]
Public Operation '0: Only Compare 1: Compare & Highlight Differences
'[--- Region Public Variables End ---]
'--------------------------------------------------------
' Name: Function Compare [Public]
'
' Remarks: N/A
'
' Purpose: Compares differences between 2 Excel Spreadsheets
'
' Arguments:
' sWorkBookUno: Primary Excel WorkBook (with complete path)
' vSheetUno: Primary Excel Spreadsheet Name
' sWorkBookDos: Secondary Excel WorkBook (with complete path)
' vSheetDos: Secondary Excel Spreadsheet Name
'
' Return: Boolean
'
' Author: Anshoo Arora, Relevant Codes
'
' Date: 03/17/2010
'
' References: N/A
'--------------------------------------------------------
Public Function Compare(sWorkBookUno, vSheetUno, sWorkBookDos, vSheetDos)
Dim oWorkBookUno, oWorkBookDos
'New instance of Excel
Set oExcel = CreateObject("Excel.Application")
Compare = False
'Open Primary WorkBook
Set oWorkBookUno = oExcel.WorkBooks.Open(sWorkBookUno)
'Open Secondary WorkBook
Set oWorkBookDos = oExcel.WorkBooks.Open(sWorkBookDos)
'Primary WorkBook Range
arrRangeUno = oWorkBookUno.WorkSheets(vSheetUno).UsedRange.Value
'Secondary WorkBook Range
arrRangeDos = oWorkBookDos.WorkSheets(vSheetDos).UsedRange.Value
'Check using CellsFound (see below) and determine any unmatched cells
If Not CellsFound > 0 Then Compare = True
'If Operation = 0, function only runs a comparison
'If Operation = 1, function runs a comparison and highlights differences
If Not Compare Then
If Operation = 1 Then
Dim Keys, oSheetUno, oSheetDos, iRow, iCol
Keys = oDict.Keys
Set oSheetUno = oWorkBookUno.WorkSheets(vSheetUno)
Set oSheetDos = oWorkBookDos.WorkSheets(vSheetDos)
'Highlight each Row/Column combination from the dictionary
For Each iKey in Keys
iRow = CInt(Split(iKey, "|")(0))
iCol = CInt(Split(iKey, "|")(1))
'Highlight the difference in the Primary Sheet
oSheetUno.Rows(iRow).Columns(iCol).Interior.ColorIndex = 3
'Highlight the difference in the Secondary Sheet
oSheetDos.Rows(iRow).Columns(iCol).Interior.ColorIndex = 3
Next
'Save primary and secondary workbooks
oWorkBookUno.Save
oWorkBookDos.Save
'Dispose primary and secondary sheet objects
Set oSheetUno = Nothing
Set oSheetDos = Nothing
End If
End If
'Dispose primary and secondary workbook objects
oWorkBookUno.Close
oWorkBookDos.Close
End Function
'--------------------------------------------------------
' Name: Function CellsFound [Private]
'
' Remarks: N/A
'
' Purpose: Finds the dissimilar cells between 2 sheets
'
' Arguments: N/a
'
' Return: Integer
'
' Author: Anshoo Arora, Relevant Codes
'
' Date: 03/17/2010
'
' References: N/A
'--------------------------------------------------------
Private Function CellsFound()
Dim iBoundsUno, iBoundsDos, iCellUno, iCellDos
CellsFound = 0
'New instance of Scripting.Dictionary
Set oDict = CreateObject("Scripting.Dictionary")
'Get 2D upper bound for Primary Range
iBoundsUno = UBound(arrRangeUno, 2)
'Get 2D upper bound for Secondary Range
iBoundsDos = UBound(arrRangeDos, 2)
'If Range are not equal..
If iBoundsUno <> iBoundsDos Then
Reporter.ReportEvent micWarning, "Compare", "Unequal Range."
End If
'Build a Dictionary with all unmatched cells [Private oDict]
For iCellUno = 1 to UBound(arrRangeUno, 1)
For iCellDos = 1 to UBound(arrRangeUno, 2)
If arrRangeUno(iCellUno, iCellDos) <> arrRangeDos(iCellUno, iCellDos) Then
oDict.Add iCellUno & "|" & iCellDos, ""
End If
Next
Next
'Total dissimilar cells equal CellsFound
CellsFound = oDict.Count
End Function
'--------------------------------------------------------
' Name: Sub Class_Terminate [Private]
'
' Remarks: N/A
'
' Purpose: Disposes the Excel.Application object
'
' Arguments: N/A
'
' Author: Anshoo Arora, Relevant Codes
'
' Date: 03/17/2010
'
' References: N/A
'--------------------------------------------------------
Private Sub Class_Terminate()
If IsObject(oExcel) Then
If Not oExcel Is Nothing Then
Set oExcel = Nothing
End If
End If
If TypeName(oDict) = "Dictionary" Then
Set oDict = Nothing
End If
End Sub
End Class
'--------------------------------------------------------
' Name: Function CompareExcelSheets
'
' Remarks: N/A
'
' Purpose: Constructor for Class clsComparer
'
' Arguments:
' sWorkBookUno: Primary Excel WorkBook (with complete path)
' vSheetUno: Primary Excel Spreadsheet Name
' sWorkBookDos: Secondary Excel WorkBook (with complete path)
' vSheetDos: Secondary Excel Spreadsheet Name
' Operation: 0: Compare Only 1: Compare & Highlight Differences
'
' Return: Boolean
'
' Author: Anshoo Arora, Relevant Codes
'
' Date: 03/17/2010
'
' References: N/A
'--------------------------------------------------------
Function CompareExcelSheets(sWorkBookUno, vSheetUno, sWorkBookDos, vSheetDos, Operation)
Dim oClass
Set oClass = New clsComparer
oClass.Operation = Operation
CompareExcelSheets = oClass.Compare(sWorkBookUno, vSheetUno, sWorkBookDos, vSheetDos)
Set oClass = Nothing
End Function
相關文章
- 比較兩個資料庫的差異資料庫
- 比較兩個表的資料差別
- python讀取兩個excel資料檔案輸出整理好以後的excel資料檔案PythonExcel
- 比較兩個檔案,求出不同的內容,A-B
- 兩個coca略有不同詞頻檔案 比較
- shell指令碼——比較兩個檔案大小、許可權指令碼
- 在Linux中,如何比較兩個檔案差異?Linux
- 如何比較兩個資料庫表結構的不同資料庫
- oracle資料庫兩表資料比較Oracle資料庫
- java比較mysql兩個資料庫中差異JavaMySql資料庫
- .NET CORE下最快比較兩個檔案內容是否相同的方法
- 比較兩個TABLE的資料是否完全一致
- 比較兩個資料庫的表結構差異(轉)資料庫
- Java 兩個日期比較Java
- linux diff比較2個檔案的不同Linux
- 如何在RFT中比較兩個影像檔案?
- JavaScript比較兩個時間JavaScript
- 比較兩個table是否相同
- python 兩個資料夾裡的檔名對比Python
- grep -vFf 比較2個檔案差異
- .VBS字尾檔案
- 檔案內容比較
- 比較兩個陣列是否相等陣列
- JavaScript比較兩個時間大小JavaScript
- shell比較兩個字串是否相等字串
- mysql比較兩個日期間隔MySql
- javascript比較兩個時間日期的大小JavaScript
- 比較兩個的表結構差異
- 比較檔案是否相同,(比較MD5值)
- linux 中尋找檔案 的2個命令比較Linux
- 比較兩個mysql資料庫裡面的表是否相同的一個校驗指令碼MySql資料庫指令碼
- 比較輸入兩個版本號大小
- C#比較兩個字串的相似度【轉】C#字串
- 大資料檔案格式比較:AVRO vs. PARQUET vs. ORC大資料VR
- VisualDiffer for Mac(資料夾和檔案比較工具)v1.8.4Mac
- Python實現對比兩個Excel資料內容並標出不同PythonExcel
- 實用的檔案和影像比較工具:kaleidoscope mac版Mac
- excel 字元比較函式Excel字元函式