首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在excel中统计点击次数

在excel中统计点击次数
EN

Stack Overflow用户
提问于 2010-03-20 20:26:55
回答 1查看 9.1K关注 0票数 0

有人可以推荐一些计算单元格内点击次数的免费程序吗?

例如,假设这样的情况:我单击A1单元格,值显示为1,然后再次单击A1单元格,值显示为2,依此类推,如果我单击A3单元格,则单元格的单击计数显示为1,以此类推

如果像这样的事情可以实现一个宏在excel (2003请),请建议或任何其他免费程序,你可能知道,请让我知道。非常感谢您的帮助,并提前向您表示感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-03-21 09:41:10

Excel没有用于鼠标左键单击的工作表事件。

它确实有一个'SelectionChange‘的事件,这个事件可以与一个API调用结合起来,检查是否点击了鼠标左键。

这段代码需要进入Project Explorer区域中的Sheet对象,以获得您要创建的工作表。

代码语言:javascript
复制
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Key As Integer

If Target.Count > 1 Then Exit Sub
''//If multiple cells selected with left click and drag
''// then take no action

Key = GetKeyState(MOUSEEVENTF_LEFTDOWN)

If Key And 1 Then
    If IsNumeric(Target.Value) Then
        Target.Value = Target.Value + 1
''//Check to see if cell contains a number, before
''// trying to increment it
        Application.EnableEvents = False
            Target.Resize(1, 2).Select
        Application.EnableEvents = True
''//Resize the selection, so that if the cell is clicked
''// for a second time, the selection change event is fired again
    End If
End If

End Sub

尽管此代码可以工作,但即使用户没有单击鼠标左键,它也可以递增单元格值。

如果可能的话,我建议使用'BeforeDoubleClick‘事件。这是内置于Excel中的代码,比上面的代码更可靠。

为了增加单元格值,用户需要双击该单元格。

这段代码需要进入Project Explorer区域中的Sheet对象,以获得您要创建的工作表。

代码语言:javascript
复制
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

If IsNumeric(Target.Value) Then
        Target.Value = Target.Value + 1
''//Check to see if cell contains a number, before
''// trying to increment it
        Application.EnableEvents = False
            Target.Resize(1, 2).Select
        Application.EnableEvents = True
''//Resize the selection, so that if the cell is clicked
''// for a second time, the selection change event is fired again
        Cancel = True
''//Stop the cell going into edit mode
End If

End Sub
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2482979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档