当前位置: 代码迷 >> VBA >> vba代码,大家看看这段代码有什么有关问题怎么修正
  详细解决方案

vba代码,大家看看这段代码有什么有关问题怎么修正

热度:7501   发布时间:2013-02-26 00:00:00.0
vba代码,大家看看这段代码有什么问题如何修正
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
  相关解决方案