Private Sub compare_Click()
Dim file_name As String
Dim wb As Workbook
Dim row_num As Integer
Dim col_num As Integer
Dim ori_cell As Range
Dim list_cell As Range
file_name = "test_compare.xls"
Set wb = Workbooks.Add
wb.SaveAs file_name
ThisWorkbook.Sheets(1).Range("A5:AS158").Copy wb.Sheets(1).Range("A1:AS154")
wb.Sheets(1).Range("A5:AS154").ClearContents
For row_num = 9 To 10
For col_num = 1 To 15
Set ori_cell = ThisWorkbook.Sheets(1).Cells(row_num, col_num)
Set list_cell = ThisWorkbook.Sheets(1).Cells(row_num, 15 + col_num)
If StrComp(CStr(ori_cell.Value), CStr(list_cell.Value)) <> 0 Then
'wb.Sheets(1).Range("S51") = 12345
wb.Sheets(1).Range(Cells(row_num, col_num + 15), Cells(row_num, col_num + 30)).Interior.ColorIndex = 3
End If
Next col_num
Next row_num
End Sub
主要是 加亮那部分的问题, 希望各位高手能帮忙解决!
------解决方案--------------------------------------------------------
加亮的这部分有问题,wb.Sheets(1).Range(Cells(row_num, col_num + 15), Cells(row_num, col_num + 30)).Interior.ColorIndex = 3
在这种情况下用Range时,不能只给单元格,而是要给单元格的地址。
改成下面的就可以了。
wb.Sheets(1).Range(Cells(row_num, col_num + 15).Address, Cells(row_num, col_num + 30).Address).Interior.ColorIndex = 3