Excel VBA中的BeforeOneClick事件

我正在寻找像这样的东西:
Private Sub Worksheet_Before One Click(ByVal Target As Range, Cancel As Boolean)

每当我用“A1”单元格上的左键单击一次时,我想将其值从“是”改为“否”,从“否”改为“是”(取决于点击之前的值)。

问题是Excel中没有类似Worksheet_Before的事件单击Excel VBA中的事件仅用于双击,右键单击和select更改(单击左键单击)。 但请注意,保持点击一次A1,我不会改变select。

我可以做这个:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'do my code here Range(" A2 ").Select 'so I can again click on A1

有更好的解决方法吗?

下面的备选scheme4似乎是满足选举委员会要求的最佳select。

Excel中没有“Worksheet_BeforeOneClick”事件。 但是,您可以创build一个标签,将其放在问题单元格的顶部,并将一个macros分配给Label_Click事件,该事件可更改标签的标题和/或单元格的值:

选项1

第1步:在问题的工作表上创build一个ActiveX标签。

步骤2:使用以下临时子将标签放置在您想要的位置:

 Sub PlaceLabel() 'Use this sub to place "Label1" where you want it. Change the Worksheet 'reference ("Sheet1") and the Range reference ("B2") as necessary. With Label1 .Left = Worksheets("Sheet1").Range("B2").Left + 1 .Top = Worksheets("Sheet1").Range("B2").Top + 1 .Height = Worksheets("Sheet1").Range("B2").Height - 1 .Width = Worksheets("Sheet1").Range("B2").Width - 1 .Caption = "Yes" .BackStyle = fmBackStyleTOpaque End With End Sub 

步骤3:将下面的代码添加到问题的工作表中:

 Private Sub Label1_Click() 'Use this sub to change the caption of Label1 as necessary. If you rename 'the label, you will need to replace "Label1" both in the code and in the 'name of the sub with the new label name. If Label1.Caption = "Yes" Then Label1.Caption = "No" Worksheets("Sheet1").Range("B2").Value = "No" GoTo EndOfSub ElseIf Label1.Caption = "No" Then Label1.Caption = "Yes" Worksheets("Sheet1").Range("B2").Value = "Yes" End If EndOfSub: End Sub 

正如注释部分所述,如果标签被重新命名,则“标签1”将需要在代码和子名称中被replace。

这将允许您引用单元格的值或标签的标题。

选项2 – 显示单元格格式(如果需要)

第1步:在问题的工作表上创build一个ActiveX标签。

步骤2:使用以下临时子将标签放置在您想要的位置:

 Sub PlaceLabel() 'Use this sub to place "Label1" where you want it. Change the Worksheet 'reference ("Sheet1") and the Range reference ("B2") as necessary. With Label1 .Left = Worksheets("Sheet1").Range("B2").Left + 1 .Top = Worksheets("Sheet1").Range("B2").Top + 1 .Height = Worksheets("Sheet1").Range("B2").Height - 1 .Width = Worksheets("Sheet1").Range("B2").Width - 1 .Caption = "" .BackStyle = fmBackStyleTransparent End With End Sub 

步骤3:将下面的代码添加到问题的工作表中:

 Private Sub Label1_Click() 'Use this sub to change the value of the cell in question as necessary. 'Rename the Worksheet and Range as necessary. With Worksheets("Sheet1").Range("B2") If .Value = "Yes" Then .Value = "No" GoTo EndOfSub ElseIf .Value = "No" Then .Value = "Yes" End If EndOfSub: End With End Sub 

此选项使用透明标签,以便单元格的格式将被显示,而不是标签。 但是,您需要将光标从标签上移开才能显示。

scheme3

你提到在你的原帖中右键点击,但没有说明为什么你不会使用这个选项。 下面是一个如何使用右键单击事件来实现你想要的而不用打开右键菜单的例子:

 Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean) 'This sub changes the value of cell "B2" from No to Yes or from Yes to No as necessary 'on right click. It also disables the right-click menu for that cell only if the value 'of the cell is either Yes or No. If Target.Column = 2 And Target.Row = 2 Then If Target.Value = "No" Then Target.Value = "Yes" Cancel = True ElseIf Target.Value = "Yes" Then Target.Value = "No" Cancel = True End If End If End Sub 

然后,您可以在“数据validation”下添加input消息,以指示用户右键单击:

在这里输入图像描述

scheme4

没有办法使用VBA将您的select设置为Nothing ,但是可以使用一种解决方法。

第1步:在问题的工作表上创build一个ActiveX标签,并将其放置在工作表的左上angular。 (它可以放在任何地方,但是如果你把它放在左上angular,如果需要的话会更容易find)

步骤2:通过右键单击标签来设置标签的以下属性,或在select标签时单击“开发人员”选项卡下的“属性”:

 (Name) ReadyLabel BackStyle 0 - fmBackStyleTransparent Caption '(Set to empty) Enabled False PrintObject False Visible False 

步骤3:将下面的代码添加到问题的工作表中:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Column = 4 And Target.Row = 4 Then Application.ScreenUpdating = False If Target.Value = "No" Then Target.Value = "Yes" ActiveSheet.Range("A1").Select ReadyLabel.Visible = True ReadyLabel.Select ReadyLabel.Visible = False Application.ScreenUpdating = True ElseIf Target.Value = "Yes" Then Target.Value = "No" ActiveSheet.Range("A1").Select ReadyLabel.Visible = True ReadyLabel.Select ReadyLabel.Visible = False Application.ScreenUpdating = True End If End If End Sub 

此选项将允许您单击有问题的单元格(该示例使用单元格“D4”)将值从“是”更改为“否”。 它也允许您点击任意次数的数值,而无需右键单击或在其他地方单击,也不会显示其他单元格被激活。 希望这可以满足您的要求。

如果您的“是/否”单元格在单元格“A1”中,则需要稍微修改该代码才能工作。 另外,您最初需要在单元格中input“是”或“否”。