VBA运行时错误91在excel文件比较代码



有一个运行时错误。下面的代码将比较两个excel。我想检查所有比较的元素,不同的单元格值将在file2中被着色。

但是每次我运行它都得到91。

Sub Compare()
Dim sh As Integer, ShName As String
Dim F1_Workbook As Workbook, F2_Workbook As Workbook
Dim iRow As Double, iCol As Double, iRow_Max As Double, iCol_Max As Double
Dim File1_Path As String, File2_Path As String, F1_Data As String, F2_Data As String

File1_Path = ThisWorkbook.Sheets(1).Cells(2, 2)
File2_Path = ThisWorkbook.Sheets(1).Cells(3, 2)
iRow_Max = ThisWorkbook.Sheets(1).Cells(4, 2)
iCol_Max = ThisWorkbook.Sheets(1).Cells(5, 2)
Set F2_Workbook = Workbooks.Open(File2_Path)
Set F1_Workbook = Workbooks.Open(File1_Path)

ThisWorkbook.Sheets(1).Cells(7, 2) = F1_Workbook.Sheets.Count 'At this point, the code say 91 rte

For sh = 1 To F1_Workbook.Sheets.Count
ShName = F1_Workbook.Sheets(sh).Name
ThisWorkbook.Sheets(1).Cells(7 + sh, 1) = ShName
ThisWorkbook.Sheets(1).Cells(7 + sh, 2) = "Identical Sheets"
ThisWorkbook.Sheets(1).Cells(7 + sh, 2).Interior.Color = vbGreen

For iRow = 1 To iRow_Max
For iCol = 1 To iCol_Max
F1_Data = F1_Workbook.Sheets(ShName).Cells(iRow, iCol)
F2_Data = F2_Workbook.Sheets(ShName).Cells(iRow, iCol)


If F1_Data <> F2_Data Then
F1_Workbook.Sheets(ShName).Cells(iRow, iCol).Interior.Color = vbRed
ThisWorkbook.Sheets(1).Cells(8 + sh, 2) = "Mismatch Found"
ThisWorkbook.Sheets(1).Cells(8 + sh, 2).Interior.Color = vbRed
End If
Next iCol
Next iRow
Next sh

End Sub

我发现了错误。两个文件的名字是一样的

相关内容

  • 没有找到相关文章

最新更新