通过点击在单元格中插入字符

如何在Excel单元格中插入一个字符只需点击它? 再次单击单元格将从单元格中删除该字符。

例如:我有一个单元格区域,A1:A10。 如果我点击这个范围内的一个单元格,那么在这个特定的单元格上插入一个“X”,再次点击它将删除“X”。

这可以通过分配给macros的button来完成吗? (该button将被分配给特定的单元格)

我有一个单元格区域,A1:A10。 如果我点击这个范围内的一个单元格,那么在这个特定的单元格上插入一个“X”,再次点击它将删除“X”。

当您使用Worksheet_BeforeDoubleClick时,必须非常小心。 如果在代码中没有注意到这一点,那么只要有双击,代码就会触发。 另外使用Cancel = True否则你可能会得到不希望的结果。

这是你正在尝试?

 Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _ Cancel As Boolean) '~~> Check if the double click happened in Range A1:A10 '~~> Also notice the placement of `Cancel = True` If Not Intersect(Target, Range("A1:A10")) Is Nothing Then '~~> Check if the cell is empty If Len(Trim(Target)) = 0 Then Target.Value = "X" Cancel = True '~~> Check if the cell already has X. This is to ensure that '~~> the cell is not cleared if the user has typed something '~~> else in the cell. ElseIf UCase(Trim(Target)) = "X" Then Target.ClearContents Cancel = True End If End If End Sub 

我会build议使用双击进入/删除值。 如果你可以使用双击..然后这里是代码:

 Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean) If IsEmpty(Target.Cells) Then Target.Cells.Value = "x" Else: Target.Cells.Clear End If Cancel = True End Sub 

代码需要放在ThisWorkbook对象中

这似乎不是一个好方法。 为什么不使用下拉菜单? 进入数据 – >validationselect一个列表。 您可以从Excel范围中select您的选项列表,或者简单地指定像这样的“Item1; Item2”等列表

使用任何VBA将防止在Excel中使用Undo函数的意义 – 任何时候在Excel中执行macros的所有撤消历史logging都将被清除。 进行更改时非常不方便。