使用数组比较两组数据时出错

时间:2011-09-14 11:30:02

标签: excel excel-vba vba

在excel 2010中,我必须比较相同基础数据的两个版本(旧版本,新版本)

我使用数组来存储旧值和新值,然后尝试比较相应的值。如果值不相同,则单元格背景将变为黄色。

一切正常,直到声明

If vaNewValues(i, j).Value <> vaOldValues(i, j).Value Then

在此声明中,我得到运行时错误'424':需要对象

Sub File_Comparison()

'Open the old file

OldFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please open old file")
Workbooks.Open Filename:=OldFN
End If

'Store the range of cells into an array 'vaOldValues

vaOldValues = Range("b9:r54").Value

'Close old file

ActiveWorkbook.Close False

'Open the new file
NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please open new file")
Workbooks.Open Filename:=NewFN

'Store the range of cells into an array 'vaNewValues
vaNewValues = Range("b9:r54").Value

'i= row
'j=column

For i = 1 To UBound(vaNewValues, 1) 'max row number
For j = 1 To UBound(vaNewValues, 2) 'max column number


*If vaNewValues(i, j).Value <> vaOldValues(i, j).Value Then*

Range("b9:r54").Cells(i, j).Interior.Color = 65535

End If

Next j
Next i

End Sub

1 个答案:

答案 0 :(得分:2)

使用数组与范围不同,请尝试使用数组来访问数组中的值

If vaNewValues(i, j) <> vaOldValues(i, j) Then