Re reading the OP, I see that you don't want a function that will toggle based on cell values, but will respond the the user clicking on a cell.
Try this.
Put this UDF in a normal module
Function CycleOnClick(ParamArray CycleValues() As Variant)
Application.Volatile
Dim CycleIndex As Long, cValues As Variant
If UBound(CycleValues) < 1 Then cValues = Array(True, False) Else cValues = CycleValues
CycleIndex = 0
On Error Resume Next
With Application.Caller
CycleIndex = Evaluate(.Parent.Names(Replace(.Address, "$", "_")).RefersTo)
End With
CycleOnClick = cValues(CycleIndex Mod (UBound(cValues) + 1))
End Function
Then put this in the ThisWorkbook code module
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim CycleIndex As Long, formStr As String
With Target
If LCase(.Formula) Like "=cycleonclick*" Then
Cancel = True
On Error Resume Next
CycleIndex = Evaluate(.Parent.Names(Replace(.Address, "$", "_")).RefersTo)
On Error GoTo 0
.Parent.Names.Add(Name:=Replace(.Address, "$", "_"), RefersTo:="=" & CycleIndex + 1).Visible = False
.Calculate
End If
End With
End Sub
Put =CycleOnClick() in a cell, double clicking on that cell will toggle between TRUE and FALSE
=CycleOnClick("a", "b", "c", "d") will cycle through those letters.
One can use formulas as arguments.
=CycleOnClick("Sin=" & Sin(A1), "Cos=" & Cos(A1))
One can put multiple cycles (of differing lengths) in the same cell
=CycleOnClick("A", "B", "C") & CycleOnClick(1, 2, 3, 4)
each double click will advance both of the cycles i.e. A1 > B2 > C3 > A4 > B1 > ... > B3 > C4 > A1 > ...
Bookmarks