用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
- 在Linux中,如何比較兩個檔案差異?Linux
- 兩個coca略有不同詞頻檔案 比較
- 如何比較兩個資料庫表結構的不同資料庫
- .NET CORE下最快比較兩個檔案內容是否相同的方法
- java比較mysql兩個資料庫中差異JavaMySql資料庫
- python 兩個資料夾裡的檔名對比Python
- 比較兩個table是否相同
- JavaScript比較兩個時間JavaScript
- 檔案內容比較
- pandas比較兩個文件的差異
- Javers 比較兩個類的差異
- 比較檔案是否相同,(比較MD5值)
- VisualDiffer for Mac(資料夾和檔案比較工具)v1.8.4Mac
- 實用的檔案和影像比較工具:kaleidoscope mac版Mac
- 比較兩個陣列是否相等陣列
- VisualDiffer for mac (檔案比較工具)Mac
- Python實現對比兩個Excel資料內容並標出不同PythonExcel
- 大資料檔案格式比較:AVRO vs. PARQUET vs. ORC大資料VR
- Beyond Compare for Mac(檔案比較對比工具)Mac
- matlab比較兩個矩陣是否相等Matlab矩陣
- python如何比較兩個字串是否相等Python字串
- C#比較兩個字串的相似度【轉】C#字串
- 多個excel檔案合併成一個excel表的方法 如何快速合併多個excel檔案Excel
- UltraCompare for Mac「Macos檔案比較工具」Mac
- Kaleidoscope for Mac,檔案影像比較工具Mac
- 檔案和影像比較工具Kaleidoscope
- UltraCompare 21 for Mac 檔案比較工具Mac
- 在 Linux 中用 Meld以圖形方式比較檔案和資料夾Linux
- EXCEL,POI,EASYEXCEL的使用和比較Excel
- Python與Excel VBA比較PythonExcel
- excel 字元比較函式Excel字元函式
- 檔案比較工具:VisualDiffer for mac v1.8.8啟用版Mac
- 檔案比較工具:VisualDiffer for mac v1.8.9啟用版Mac
- VisualDiffer for mac (檔案比較工具) v1.8.0啟用版Mac
- excel視窗獨立開啟不重疊 兩個excel檔案怎麼實現兩個視窗顯示Excel
- 如何將資料庫中的資料導成 excel 檔案資料庫Excel
- matlab輸出資料為excel檔案MatlabExcel