我的Excel工作表包含20多个电子表格,每个电子表格包含1000多行。我在Linux环境中使用python程序生成此Excel工作表。 但我也可以把这些文件带到windows环境中。是否可以将两个Excel工作表与电子表格数据进行比较?有没有人使用Windows,Linux和Excel属性或Python中的任何工具来比较电子表格表和数据
答案 0 :(得分:1)
VBA是最明显的解决方案 将每个工作表的使用范围加载到一个数组中并循环遍历该数组 不要遍历范围,因为这需要时间。
动态编写的一些代码作为其工作原理的基本示例。 根据您要比较的方式和内容,您可以进行自定义。无论如何,这只是一般原则。
Dim lCnt_A as Long
Dim lCnt_B as Long
Dim vArray_1 as variant
Dim vArray_2 as variant
Dim oRange_1 as Excel.range
Dim oRange_2 as Excel.range
Dim lCnt_Rows as Long
Dim lCnt_Cols as Long
'Don't do this:
for lCnt_A = 1 to 5000
for lCnt_B = 1 to 100
if thisworkbook.sheets(1).cells(lCnt_A, lCnt_B).value <> Thisworkbook.sheets(2).cells(lCnt_A, lCnt_B).value Then
Msgbox ("Difference spotted in cell" & thisworkbook.sheets(1).cells(lCnt_A, lCnt_B).Address)
end if
next lCnt_B
next lCnt_A
'But rather this, it's faster:
set oRange_1 = thisworkbook.sheets(1).usedrange
set oRange_2 = thisworkbook.sheets(2).usedrange
lCnt_Rows = oRange_1.rows.count
lCnt_Cols = oRange_1.rows.count
redim vArray_1(1 to lCnt_Rows, 1 to lCnt_Cols)
redim vArray_2(1 to lCnt_Rows, 1 to lCnt_Cols)
vArray_1 = oRange_1
vArray_2 = oRange_2
for lCnt_A = 1 to 5000
for lCnt_B = 1 to 100
if vArray_1(lCnt_A, lCnt_B) <> vArray_2(lCnt_A, lCnt_B) then
Msgbox ("Difference spotted in row " & lCnt_A & " and column " & lCnt_B)
end if
next lCnt_B
next lCnt_A
答案 1 :(得分:1)
我同意没有谷歌的负面观点。有时候我犯了这个错误。非常感谢您的回复。但我发现以下工具比较两个Excel.anyway它正在编程。只是FYi如果您正在寻找这样的工具。