跳到主要内容

如何计算Excel中指定单元格中的总点击次数?

本文讨论的是计算Excel中指定单元格中的总点击次数。

使用VBA代码计算指定单元格中的总点击次数


使用VBA代码计算指定单元格中的总点击次数

请执行以下操作以计算Excel中指定单元格中的总点击次数。

1.在工作表中包含您需要计算其总点击次数的单元格,右键单击工作表选项卡,然后单击 查看代码 从上下文菜单。

2.在 Microsoft Visual Basic应用程序 窗口,请将以下VBA代码复制并粘贴到“代码”窗口中。

VBA代码:计算Excel中指定单元格中的总点击次数

Public xRgS, xRgD As Range
Public xNum As Long
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error Resume Next
    If Target.Cells.Count > 1 Then Exit Sub
    Set xRgS = Range("E2")
    If xRgS Is Nothing Then Exit Sub
    Set xRgD = Range("H2")
    If xRgD Is Nothing Then Exit Sub
    If Intersect(xRgS, Target) Is Nothing Then Exit Sub
    xNum = xNum + 1
    xRgD.Value = xNum
End Sub

备注:在代码中,E2是您需要计算其总点击次数的单元格,而H2是该计数的输出单元格。 请根据需要更改它们。

3。 按 其他 + Q 关闭键 Microsoft Visual Basic应用程序 窗口。

从现在开始,在此指定的工作表中单击单元格E2时,总点击次数将自动填充到单元格H2中,如下图所示。 例如,如果您单击单元格E2 5次,则数字5将显示在单元格H2中。

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (31)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi.
thanks for your amazing code.
as i use this, the counter get restarted every time i open the file,
is there any solution for this issue?
i need to see thclick counts in a larger time window

thanks in advance
This comment was minimized by the moderator on the site
Hi Mehrdad,
I am sorry to reply to you so late. The following code can help solve your problem. Every time you open the file, the counter will start counting from the last counted number.

Public xRgS, xRgD As Range
'Updated by Extendoffice 20230407
Public xNum As Long
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error Resume Next
    If Target.Cells.Count > 1 Then Exit Sub
    Set xRgS = Range("E2")
    If xRgS Is Nothing Then Exit Sub
    Set xRgD = Range("H2")
    If xRgD Is Nothing Then Exit Sub
    If Intersect(xRgS, Target) Is Nothing Then Exit Sub
    xNum = xRgD.Value
    xNum = xNum + 1
    xRgD.Value = xNum
End Sub
This comment was minimized by the moderator on the site
Hola. Muchas gracias por los códigos.
Me gustaría saber cómo contar las veces que se hace clic sobre un enlace en una celda.
Muchas gracias.
This comment was minimized by the moderator on the site
Hi jose maria,
To count the clicks on a hyperlink, you can try the following VBA code.
Suppose hyperlinks are in column A and you want the number of clicks to be populated in the corresponding cell of column B (as shown in the screenshot below)
Please put the following code in the worksheet (code) window.

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
'Updated by Extendoffice 20220805
    Dim Hyperlink As Range
    Set Hyperlink = Target.Range

    Hyperlink.Offset(0, 1) = Hyperlink.Offset(0, 1) + 1
End Sub

https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/clicks_on_a_hyperlink.png
This comment was minimized by the moderator on the site
Bonjour ,
j'aimerai comment je pourrais le nombre de clics sur les cellules D10 à M10 et le retranscrire à la ligne R10 et le faire pour toutes les lignes suivante donc compter les clics sur les cellules D11 à M11 et le transcrire à la ligne R11 etc etc ?

Cordialement
This comment was minimized by the moderator on the site
Hi DUFOUR,
To count the number of clicks from D10 to M10 and output the total number of clicks in R10, you can apply the following VBA code to get it done.
Note: In the code, the range "D10:M30" means that the code only works from the row 10 to row 30, so please specify the rows you want to count.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Updated by Extendoffice 20220609
    Dim xNum As Long
    Dim xRgCount, xRg As Range
    
    On Error Resume Next
    If Target.Cells.Count > 1 Then Exit Sub

    Set xRg = Range("D10:M30")
    If Intersect(xRg, Target) Is Nothing Then Exit Sub
    Set xRgCount = Range("R" & Target.Row)
    
    If IsNumeric(xRgCount.Value) Then
        xNum = xRgCount.Value + 1
    Else
        xNum = 1
    End If
    xRgCount.Value = xNum
End Sub
This comment was minimized by the moderator on the site
Is there a way to backtrack the number count? For exemple: I'd made 5 clicks, but I just wanted 3. So I change the number in the cell to 3, and when I click again, it continues from 3. OR have the ability to press another cell and decrease the count by 1 if that is easier.
This comment was minimized by the moderator on the site
Hi, thank you for these VBA codes, they almost work for my needs. I fear the fact I need to go past double digits means it will not work. I need to have C8 through to C110 and the corresponding tally count being L8 through to L110. Can you help? Many thanks in advance.
This comment was minimized by the moderator on the site
Hi Andy,The following VBA code can do you a favor. Please have a try.<div data-tag="code">Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRgS, xRgD As Range
Dim xStrRg As String
Dim xCStr, xVStr As String
Dim xItem As Integer
xCStr = "C8:C110" 'The range of cells you want to record the clicks of each cell
xVStr = "L8:L110" 'The range of cells to place the records in
Set xRgS = Range(xCStr)
Set xRgD = Range(xVStr)
If Not (Intersect(xRgS, Target) Is Nothing) Then
xItem = Target.Row - xRgS.Item(1).Row + 1
xRgD.Item(xItem).Value = xRgD.Item(xItem).Value + 1
End If
End Sub
This comment was minimized by the moderator on the site
Hi, I'm trying to find a way of counting the number of times 20 different cells are being clicked (each one should be counted separately). I came across your VBA code suggestion, tried to adjust it to my specific needs but it won't work. can you please advise how the code should be written? the cells that I would like to count and the cells that the values should appear in are: F12>AU12, F13>AU13, G12>AV12, G13>AV13, H10>AW10, H11>AW11, H12>AW12, H13>AW13, H14>AW14, H15>AW15, I10>AX10, I11>AX11, I12>AX12, I13>AX13, I14>AX14, I15>AX15, J12>AY12, J13>AY13, K12>AZ12, K13>AZ13).
This is the VBA code I've tried with no success:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRgArray As Variant
Dim xNum
Dim xStrR, xStrS, xStrD As String
Dim xRgS, xRgD As Range

Dim xFNum As Long
xRgArray = Array("F12,AU12", "F13,AU13", "G12,AV12", "G13,AV13", "H10,AW10", "H11,AW11", "H12,AW12", "H13,AW13", "H14,AW14", "H15,AW15", "I10,AX10", "I11,AX11", "I12,AX12", "I13,AX13", "I14,AX14", "I15,AX15", "J12,AY12", "J13,AY13", "K12,AZ12", "K13,AZ13")
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
For xFNum = LBound(xRgArray) To UBound(xRgArray)
xStrR = xRgArray(xFNum)
xStrS = ""
xStrS = Left(xStrR, 2)
xStrD = ""
xStrD = Right(xStrR, 2)
Set xRgS = Nothing
Set xRgS = Range(xStrS)
If TypeName(xRgS) <> "Nothing" Then
Set xRgD = Nothing
Set xRgD = Range(xStrD)
If TypeName(xRgD) <> "Nothing" Then
If TypeName(Intersect(xRgS, Target)) <> "Nothing" Then
xRgD.Value = xRgD.Value + 1
End If
End If
End If
Next
End Sub

Thank you in advance, for your help.
This comment was minimized by the moderator on the site
Hi,The below code can help. Please have a try. Thank you.<div data-tag="code">Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRgS, xRgD As Range
Dim xStrRg As String
Dim xFNum As Integer
Dim xArr1, xArr2
If Target.Cells.Count > 1 Then Exit Sub
xStrRg = "F12-AU12; F13-AU13; G12-AV12; G13-AV13; H10-AW10; H11-AW11; H12-AW12; H13-AW13; H14-AW14; H15-AW15; I10-AX10; I11-AX11; I12-AX12; I13-AX13; I14-AX14; I15-AX15; J12-AY12; J13-AY13; K12-AZ12; K13-AZ13"
On Error Resume Next
xArr1 = Split(xStrRg, ";")
For xFNum = 0 To UBound(xArr1)
xArr2 = Split(xArr1(xFNum), "-")
Set xRgS = Range(xArr2(0))
Set xRgD = Range(xArr2(1))
If Not (Intersect(xRgS, Target) Is Nothing) Then
xRgD.Value = xRgD.Value + 1
End If
Next
End Sub
This comment was minimized by the moderator on the site
Crystal, The Above code is great for the sheet I am working with, thank you. But I have a question about adding a time macro so that everyday (excluding weekends) the tally moves to the next row in the sheet for example:

Row 3 - 7/1/2021 "B1-B3; C1-C3; D1-D3"
Row 4 - 7/2/2021 "B1-B4; C1-C4; D1-D4"
Row 5 - 7/3/2021 "B1-B5; C1-C5; D1-D5"

If this is possible? thx, Ken
This comment was minimized by the moderator on the site
The Above corrected code is great for the sheet I am working with, thank you. But I have a question about adding a time macro so that everyday (excluding weekends) the tally moves to the next row in the sheet for example:
Row 3 - 7/1/2021 "B1-B3; C1-C3; D1-D3"Row 4 - 7/2/2021 "B1-B4; C1-C4; D1-D4"Row 5 - 7/3/2021 "B1-B5; C1-C5; D1-D5"
This comment was minimized by the moderator on the site
Como zerar a contagem? How to reset the score?
This comment was minimized by the moderator on the site
Hi,
If you want to reset the counter, please add the below VBA code at the end of the original code which has been provided above, and then run it.

Sub ClearCount()
xRgD.Value = ""
xNum = 0
End Sub
This comment was minimized by the moderator on the site
Can you provide a code that allows counting clicks from A2, B2 cells through A14, B14 cells. Thanks in advance.
This comment was minimized by the moderator on the site
Hi Barbara,
Do you mean counting the total clicks in range A2:B14? Or clicks for each cell in range A2:B14?
This comment was minimized by the moderator on the site
Hola
Hay alguna manera de programar el conteo de clicks de acuerdo a la fecha, es decir programar varias celdas para que cuenten con la fecha del día?
This comment was minimized by the moderator on the site
Hello, there is a way to back the counting for any number that I want? For exemple: I'd made 5 clicks, but i just wanted 3. So I change the number in the cell to 3, and when I click again, it continue from 3.
Thank for the code!
This comment was minimized by the moderator on the site
Hi,
Sorry can’t help you with this, welcome to post any question about Excel to our forum: https://www.extendoffice.com/forum.html. You will get more Excel supports from our professional or other Excel fans.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations