选择下一个符合条件的值

时间:2018-11-12 07:35:06

标签: excel vba excel-vba

我想在“ A”列中逐个选择单元格,该单元格用红色填充(颜色代码= 3)。每次单击按钮时,选择项应移至下一个用红色填充的单元格(同一列)。

到目前为止,我的代码:

Sub FindNext()
    Dim c As Range

    On Error Resume Next

    With Worksheets(1).Range("A1:B500")
        Set c = .Find(Cells.Interior.ColorIndex = 3, LookIn:=xlValues)
        If Not c Is Nothing Then
            Do
                c.Select
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With
End Sub

2 个答案:

答案 0 :(得分:2)

首先,红色的代码为255。如果您表示其他代码,则只需更改以下代码。

通过简单的for循环,您可以获取列中的下一个红色单元格:

Option Explicit

Sub test()

Dim Srow As Long
Dim LastRow As Long
Dim i As Long

LastRow = Range("A" & Rows.Count).End(xlUp).Row
Srow = Selection.Row

For i = Srow +1 To 500 'Replace 500 by Lastrow if your last cell has a value inside
    If Cells(i, 1).Interior.Color = 255 Then
        Cells(i, 1).Select
        Exit Sub
    End If
Next i

End Sub

答案 1 :(得分:0)

Sub Test()
Dim r As Range
If Intersect(ActiveCell, Columns("A")) Is Nothing Then [A1].Select
With Application.FindFormat
    .clear
    .Interior.ColorIndex = 3
End With
Set r = Columns("A").Find("", ActiveCell, searchformat:=True)
If Not r Is Nothing Then r.Select
End Sub

另一种可行的解决方案

相关问题