我想建立一个电子表格,当您单击一个按钮时,该电子表格将查找每行第4、7和8列中的单元格,如果三个单元格的条件正确,则将突出显示整个行。如果条件不正确,我想隐藏不正确的行。
我似乎陷入了代码选择突出显示行的位置。我曾尝试使用ActiveCell.EntireRow.Interior.Color,但是它只能高亮显示范围内的所有内容,或者高亮显示在单击按钮之前选中的任何单元格的行。
Private Sub CommandButton1_Click()
Dim rng As Range: Set rng = Application.Range("A5:I54")
Dim cell As Range
Dim row As Range
Dim i As Integer
For i = 1 To rng.Rows.Count
If rng.Cells(RowIndex:=i, ColumnIndex:=4).Text = "Yes" And rng.Cells(RowIndex:=i, ColumnIndex:=7).Text = "Yes" And rng.Cells(RowIndex:=i, ColumnIndex:=8).Text = "No" Then
rng.EntireRow.Interior.Color = vbYellow
ElseIf rng.Interior.ColorIndex = vbWhite Then rng.EntireRow.Hidden = True
End If
Next
End Sub
如何获取代码以识别行的条件并正确突出显示它?
答案 0 :(得分:1)
如果我了解您的问题,则可以通过以下代码完成:
Private Sub CommandButton1_Click()
For i = 5 To 54
If Cells(i, 4) = "Yes" And Cells(i, 7) = "Yes" And Cells(i, 8) = "No" Then
Range(Cells(i, 4), Cells(i, 9)).Interior.Color = vbYellow
ElseIf Cells(i, 4).Interior.Color = vbWhite Then
Cells(i, 4).EntireRow.Hidden = True
End If
Next
End Sub