2015-10-02 144 views
1

我正在爲工作中的文件編寫VBA,並需要做一些有點奇怪的事情。如果J列中的單元格包含特定值,我需要突出顯示該行(不是整行,只是行的已用部分)。除了我的代碼突出顯示了整行以外,我已經想出了所有的東西,我只想讓它突出顯示那一行中使用過的單元格。任何人都可以建議嗎?下面突出顯示特定行中使用的單元格

'Yellow Highlight..........THIS IS HIGHLIGHTING THE WHOLE ROW....WHY!!!!! WHY!!!!!!!!!!! 
Sheets("EMM").Activate 
    With Sheets("EMM") 
     For Lrow = 1 To ActiveSheet.UsedRange.Rows.Count 
       With .Cells(Lrow, "J") 
        If Not IsError(.Value) Then 
         If .Value = "Desk to adjust" Then 
          .EntireRow.Interior.ColorIndex = 6 
         End If 
        End If 
       End With 
     Next Lrow 
    End With 
+1

因爲.EntireRow適用的顏色與整個行...你會ahve檢查每一列和顏色行的單元格不整行.. – MatthewD

+0

'.EntireRow.Interior.ColorIndex = 6'」 s EntireRow導致整行被突出顯示。要突出顯示某個特定的單元格,請使用「Range(」A1「)。Interior.Color = RGB(255,0,0)' – zedfoxus

回答

0
With Sheets("EMM") 
    For Lrow = 1 To .UsedRange.Rows.Count 
     With .Cells(Lrow, "J") 
      If Not IsError(.Value) Then 
       If .Value = "Desk to adjust" Then 
        Sheets("EMM").UsedRange.Rows(Lrow).Interior.ColorIndex = 6 
       End If 
      End If 
     End With 
    Next 
End With 

代碼或使用自動篩選

With Sheets("EMM").UsedRange 
    .Parent.AutoFilterMode = False 
    .AutoFilter 
    .AutoFilter Field:=10, Criteria1:="Desk to adjust" 
    .Rows(1).Hidden = True  'Header row 
    .Interior.ColorIndex = 6 
    .Rows(1).Hidden = False  'Header row 
    .AutoFilter 
    .Cells(1, 1).Activate 
End With 
0

避免循環的行和取非空白單元格,或設置過濾器,最簡單的,就是做這個的IF內:

.UsedRange.Rows(lRow).Interior.ColorIndex = 6 
.UsedRange.Rows(lRow).SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 0 

(突出顯示的行然後unhighlight空細胞)

0

下面是一些評論代碼,展示瞭如何使用Range.AutoFilter方法來實現您正在查找的結果。不需要循環,因此通常效率更高:

Sub tgrFilter() 

    Dim ws As Worksheet 
    Dim rngData As Range 
    Dim strMatch As String 

    Set ws = ActiveWorkbook.Sheets("EMM")  'We will be working with sheet "EMM" in activeworkbook 
    Set rngData = ws.Range("J1").CurrentRegion 'Get the region that column J is a part of in order to limit the highlighting (so it doesn't highlight entire row) 
    strMatch = "Desk to adjust"     'This is the string/value we are looking for in column J 
    rngData.Interior.Color = xlNone    'Clear any previous highlighting 

    On Error Resume Next 'If there are no results, it would cause an error 

    'Work with just column J within rngData 
    With Intersect(rngData, ws.Columns("J")) 
     .AutoFilter 1, strMatch  'Note that the filter is not case sensitive 

     'Color the matching rows contained in the data (not the entire row) 
     Intersect(rngData, .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow).Interior.ColorIndex = 6 

     .AutoFilter  'Remove the filter 
    End With 

    If Err.Number <> 0 Then 
     'Error occurred which means there were no results 
     MsgBox "No matches found in column J for [" & strMatch & "]", , "No results" 
     Err.Clear 
    End If 

    On Error GoTo 0   'Remove the On Error Resume Next condition 

End Sub 
0

您可以使用條件格式而不是宏。

選擇單元格您需要突出,並在菜單欄去格式 - >條件格式
enter image description here

在您需要檢查對話框中選擇條件,這個例子檢查單元格的值等於「AA 」。您可以一次添加最多3個條件的條件
enter image description here

接下來,單擊格式按鈕以在條件爲真時格式化單元格。 enter image description here

完成後點擊確定關閉對話框,您將獲得所需的高光。 enter image description here

相關問題