Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to count total clicks in a specified cell in Excel?

This article is talking about counting total clicks in a specified cell in Excel.

Count total clicks in a specified cell with VBA code


Count total clicks in a specified cell with VBA code


Please do as follows to count total clicks in a specified cell in Excel.

1. In the worksheet contains the cell you need to count its total clicks, right-click the sheet tab, and then click View Code from the context menu.

2. In the Microsoft Visual Basic for Applications window, please copy and paste below VBA code into the Code window.

VBA code: Count total clicks in a specified cell in 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

Note: In the code, E2 is the cell you need to count its total clicks, and H2 is the output cell of the counting. Please change them as you need.

3. Press the Alt + Q keys to close the Microsoft Visual Basic for Applications window.

From now on, when clicking on cell E2 in this specified worksheet, the total clicks will be auto-populated in cell H2 as below screenshot shown. For example, if you click on the cell E2 5 times, number 5 will be displayed in cell H2.


Recommended Productivity Tools for Excel

kte tab 201905

Kutools for Excel Helps You Always Finish Work Ahead of Time, and Stand Out From Crowd

  • More than 300 powerful advanced features, designed for 1500 work scenarios, increasing productivity by 70%, give you more time to take care of family and enjoy life.
  • No longer need memorizing formulas and VBA codes, give your brain a rest from now on.
  • Become an Excel expert in 3 minutes, Complicated and repeated operations can be done in seconds, 
  • Reduce thousands of keyboard & mouse operations every day, say goodbye to occupational diseases now.
  • 110,000 highly effective people and 300+ world-renowned companies' choice.
  • 60-day full features free trial. 60-day money back guarantees. 2 years of free upgrade and support.

Brings Tabbed Browsing and Editing to Microsoft Office, Far More Powerful Than The Browser's Tabs

  • Office Tab is designed for Word, Excel, PowerPoint and Other Office Applications: Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Barbara · 2 months ago
    Can you provide a code that allows counting clicks from A2, B2 cells through A14, B14 cells. Thanks in advance.
    • To post as a guest, your comment is unpublished.
      crystal · 1 months ago
      Hi Barbara,
      Do you mean counting the total clicks in range A2:B14? Or clicks for each cell in range A2:B14?
  • To post as a guest, your comment is unpublished.
    Andrés · 4 months ago
    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?
  • To post as a guest, your comment is unpublished.
    Demetrius · 7 months ago
    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!
  • To post as a guest, your comment is unpublished.
    Guido · 1 years ago
    Thank you for the code, very useful.
    I'm not a programmer and I would like to know how to extend this process to every line. That is to say, not only E2>H2 but also E3>H3, E4>H4, and so on.
    Is there a code for this?


    Thank you in advance!
    • To post as a guest, your comment is unpublished.
      crystal · 10 months ago
      Hi Guido,

      The below VBA code can help you to solve the problem. Please have a try. Thanks for your comment.
      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("E2,H2", "E3,H3", "E4,H4", "E5,H5", "E6,H6")
      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
      • To post as a guest, your comment is unpublished.
        Ruth · 10 months ago
        Thanks for this. I tried and it worked, however it only worked only until certain number of cells, how can we extend this code until the end of the cells? for example i type in this code below and it only works until "G9,G9". Thanks


        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("C4,C4", "D4,D4", "E4,E4", "F4,F4", "G4,G4", "C6,C6", "D6,D6", "E6,E6", "F6,F6", "G6,G6", "C7,C7", "D7,D7", "E7,E7", "F7,F7", "G7,G7", "C8,C8", "D8,D8", "E8,E8", "F8,F8", "G8,G8", "C9,C9", "D9,D9", "E9,E9", "F9,F9", "G9,G9", "C10,C10", "D10,D10", "E10,E10", "F10,F10", "G10,G10", "C11,C11", "D11,D11", "E11,E11", "F11,F11", "G11,G11", "C14,C14", "D14,D14", "E14,E14", "F14,F14", "G14,G14", "C15,C15", "D15,D15", "E15,E15", "F15,F15", "G15,G15", "C16,C16", "D16,D16", "E16,E16", "F16,F16", "G16,G16", "C17,C17", "D17,D17", "E17,E17", "F17,F17", "G17,G17", "C18,C18", "D18,D18", "E18,E18", "F18,F18", "G18,G18", "C20,C20", "D20,D20", "E20,E20", "F20,F20", "G20,G20")
        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
        • To post as a guest, your comment is unpublished.
          crystal · 9 months ago
          Hi Ruth,
          The code is hard to optimized for meeting your needs. Sorry about that.
  • To post as a guest, your comment is unpublished.
    Dennis · 1 years ago
    How can you "reset" the counter?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear Dennis,
      Please add the below VBA code at the end of the original code. Every time you run this code, the counting will be reset to 0. Thank you for your comment.

      Sub ClearCount()
      xRgD.Value = ""
      xNum = 0
      End Sub