2016-07-22 97 views
0

我有代码来输入信息,然后将该信息与表进行比较。我想突出显示正在创建的新行和我们正在比较新行的行。这里是我的代码突出显示两行

Sub findData() 
    Dim workflow As String 
    Dim finalrow As Integer 
    Dim i As Integer 

    With Sheets("Sheet1") 
     workflow = .Range("C5").Value 
     servergri = .Range("C9").Value 
     gridf = .Range("C9").Value 
     StartTime = .Range("c11").Value 
    End With 

    With Sheets("Sheet3") 
     finalrow = .Range("C" & Rows.Count).End(xlUp).Row 

     For i = 5 To finalrow 
      If .Cells(i, 3) = workflow And (.Cells(i, 4) = servergri Or .Cells(i, 5) = gridf) Then 

       .Rows(i).Insert 
       'Add new information to the new row. 
       'The new row number is still = i 

       .Cells(i, 3) = workflow 
       .Cells(i, 4) = servergri 
       .Cells(i, 6) = StartTime 


       'If you only want to add one row then your should exit the loop 
       Exit For 
      End If 
     Next 
    End With 
    Call Worksheet_SelectionChange 
End Sub 

Sub Worksheet_SelectionChange() 
    Dim Target As Range 
    Application.ScreenUpdating = False 
    ' Clear the color of all the cells 
    Cells.Interior.ColorIndex = 0 
    ' Highlight the active cell 
    Target.Interior.ColorIndex = 8 
    Application.ScreenUpdating = True 
End Sub 
+0

你从来没有设置'Target'是任何其他比'SelectionChange'宣布它作为一个'Range'后事件... – Dave

+0

那么如何将它分配给新的和比较行? @Dave –

+0

'Private Sub Worksheet_SelectionChange(ByVal Target As Range)' – Slai

回答

0

只需添加

.Cells.Interior.ColorIndex = 0 
.Cells(i, 3).Resize(2, 4).Interior.ColorIndex = 8 

.Cells(i, 6) = StartTime