简体   繁体   中英

Excel Macro to Change Active Cell

I have created a form in Excel. Based on how the user completes one cell, changes which cells they fill out next (for example: When filling out A10 if they answer "X" they will move to B1, if they answer "Y" they will move to B3.)

In order to guide the user through the form, I created a complex set of conditional formatting rules which will "highlight"(background fill) the next cell they need to fill out. Once they complete the cell the formatting on that cell goes away and switches to the next cell.

I have the conditional formatting working exactly how I want. My question is: Is there a way to have the active cell follow this same path. Either by setting up the same formula rules that guide the conditional formatting or is there a way to have a macro auto set the active cell to the "highlighted" cell from the conditional formatting?

Try this:

Private Sub Worksheet_Change(ByVal Target As Range)
    Const NEUTRAL = 16777215
    Dim r As Range
    For Each r In Cells.SpecialCells(xlCellTypeAllFormatConditions)
        If r.DisplayFormat.Interior.Color <> NEUTRAL Then
            r.Select
            Exit For
        End If
    Next
End Sub

Note: you can edit NEUTRAL at the top if non-highlighted cells have a different color than pure white.

Note: this assumes that the cell you want the selection to jump to is the only cell on the sheet that is highlighted with conditional formatting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM