再次单击同一单元格(切换)

问题描述 投票:0回答:1

'EXCEL for Mac

需要模拟 200 个复选框,但复选框会减慢 Excel 速度。

尝试在两个自定义值之间重复切换同一单元格。 就像在活动时单击与第二次相同的单元格一样。

Application.ontime
函数在以下代码中对我不起作用: '我已允许执行所有宏。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Address = "$A$1" Or Target.Address = "$A$2" Then
        Exit Sub
    End If
    
    ' Check if the value of the active cell matches cell A1
    If ActiveCell.Value <> Range("A1").Value Then
        ' Assign the active cell to cell A1
        ActiveCell.Value = Range("A1").Value
    Else
       ActiveCell.Value = Range("B1").Value
    End If

    ' Call the CheckActiveCellValue subroutine
    'CheckActiveCellValue
    
        ' To allow triggering the event even when clicking on the same cell,
    ' you can call the event manually using Application.OnTime
    
    
    
   ' WILL NOT WORK
    'Application.OnTime Now + TimeValue("00:00:01"), "TriggerSelectionChangeX"
    'Application.OnTime TimeValue(Now() + TimeSerial(0, 1, 0)), "TriggerSelectionChangeX"

    
End Sub

Sub TriggerSelectionChangex()
    Worksheet_SelectionChange Selection
End Sub

Sub HelloWorld()

MsgBox "Hello World"
Application.OnTime TimeValue(Now() + TimeSerial(0, 1, 0)), "HelloWorld"

End Sub

在上面的示例中,在 17:00 第一次执行后,将每 1 分钟重复一次。

此代码确实适用于勾选我的自定义打开和关闭复选框图形。 但我似乎无法单击同一单元格来切换该值。 我已勾选允许所有宏执行设置。

excel vba checkbox toggle
1个回答
0
投票
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Address = "$A$1" Or Target.Address = "$A$2" Or Target.Address = "$C$1" Then
    Exit Sub
End If

' Check if the value of the active cell matches cell A1
If ActiveCell.Value <> Range("A1").Value Then
    ' Assign the active cell to cell A1
    ActiveCell.Value = Range("A1").Value
Else
   ActiveCell.Value = Range("B1").Value
End If

范围(“C1”).选择

结束子

© www.soinside.com 2019 - 2024. All rights reserved.