提示:其他語言是Google翻譯的。 你可以訪問 English 版本。
登入
x
or
x
x
註冊
x

or

如何計算單元格在Excel中更改的次數?

要計算在Excel中更改單元格的次數,本文可以幫助您。

用VBA代碼計算單元格更改的次數


用VBA代碼計算單元格更改的次數


請按照以下步驟計算單元格在Excel中更改的次數。

1。 在工作表中,您需要統計指定單元格的全部更改,右鍵單擊工作表選項卡,然後單擊 查看代碼 從上下文菜單。 看截圖:

2。 在開幕式上 Microsoft Visual Basic for Applications 窗口,請複制並粘貼下面的VBA代碼到代碼窗口。

VBA代碼:計算單元格在Excel中更改的次數

Dim xCount As Integer

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xRg As Range, xCell As Range
    On Error Resume Next
    If Target = Range("B9") Then
        xCount = xCount + 1
        Range("C9").Value = xCount                                     
    End If
    Application.EnableEvents = False
    Set xRg = Application.Intersect(Target.Dependents, Me.Range("B9"))
    If Not xRg Is Nothing Then
        xCount = xCount + 1
        Range("C9").Value = xCount
    End If
    Application.EnableEvents = True
End Sub

注意:在代碼中,B9是您需要對其更改進行計數的單元格,C9是用於填充計數結果的單元格。 請根據需要更改它們。

從現在起,當單元格B9發生更改時,其更改的總數將被重疊並自動填充到單元格C9中。



推薦的Excel生產力工具

Kutools for Excel幫助您提前完成工作,並從人群中脫穎而出

  • 超過300強大的高級功能,專為1500工作場景設計,通過70%提高生產力,讓您有更多時間照顧家庭和享受生活。
  • 不再需要記憶公式和VBA代碼,從現在起讓你的大腦休息一下。
  • 成為3分鐘的Excel專家,複雜和重複的操作可以在幾秒鐘內完成,
  • 每天減少成千上萬的鍵盤和鼠標操作,現在告別職業病。
  • 110,000高效人才和300 +世界知名公司的選擇。
  • 60-day full功能免費試用。 60天退款保證。 2多年的免費升級和支持。

將選項卡式瀏覽和編輯帶到Microsoft Office,遠比瀏覽器的選項卡強大

  • Office選項卡專為Word,Excel,PowerPoint和其他Office應用程序設計:Publisher,Access,Visio和Project。
  • 在同一窗口的新選項卡中打開並創建多個文檔,而不是在新窗口中。
  • 通過50%提高您的工作效率,每天為您減少數百次鼠標點擊!
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.
    JediTrader · 2 months ago
    Team,

    When I tried using :

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xRg As Range, xCell As Range
    Dim xSRg, xRRg As Range
    Dim xFNum As Long

    Set xSRg = Range("B9:B1000")
    Set xRRg = Range("C9:C1000")

    carefully changing the Range and Target cells vis a vis P2:P200 and X2:X200 respectively, I dont the change-count in X Column despite myself trying to change cells across multiple rows across P2:P200.

    Any help would be greatly appreciated.

    Regards
    JT
  • To post as a guest, your comment is unpublished.
    Shiju · 4 months ago
    Hello All,

    The solution as provided under "Count Number Of Times A Cell Is Changed With VBA Code" is good if we are only tracking changes to ONE CELL. Please suggest, what modifications are needed, if the tracking is to be done for multiple cells. In case of multiple cells, the incremental counter should appear next to the cell for which the change in value is being tracked.
    • To post as a guest, your comment is unpublished.
      Shiju · 4 months ago
      Looking forward for help and assistance to have a specific VBA code, which can be applied to multiple cells in one worksheet.
      • To post as a guest, your comment is unpublished.
        crystal · 4 months ago
        Hi Shiju,
        Please try the below VBA code. Thanks for commenting.

        Private Sub Worksheet_Change(ByVal Target As Range)
        Dim xRg As Range, xCell As Range
        Dim xSRg, xRRg As Range
        Dim xFNum As Long

        Set xSRg = Range("B9:B1000")
        Set xRRg = Range("C9:C1000")

        Application.EnableEvents = False
        On Error Resume Next
        For xFNum = 1 To xSRg.count
        If Target = xSRg.Item(xFNum) Then
        xRRg.Item(xFNum).Value = xRRg.Item(xFNum).Value + 1
        Application.EnableEvents = True
        Exit Sub
        End If
        Next xFNum
        Application.EnableEvents = True
        End Sub
  • To post as a guest, your comment is unpublished.
    Kevin · 1 years ago
    Did you figure this out. I too am interested in this for checking multiple cells. Haven’t tried it yet.
    • To post as a guest, your comment is unpublished.
      crystal · 11 months ago
      Hi Kevin,

      The following code can help you solving the problem. Thanks for your comment.
      Private Sub Worksheet_Change(ByVal Target As Range)
      Dim xRg As Range, xCell As Range
      Dim xSRg, xRRg As Range
      Dim xFNum As Long

      Set xSRg = Range("B9:B1000")
      Set xRRg = Range("C9:C1000")

      Application.EnableEvents = False
      On Error Resume Next
      For xFNum = 1 To xSRg.count
      If Target = xSRg.Item(xFNum) Then
      xRRg.Item(xFNum).Value = xRRg.Item(xFNum).Value + 1
      Application.EnableEvents = True
      Exit Sub
      End If
      Next xFNum
      Application.EnableEvents = True
      End Sub
  • To post as a guest, your comment is unpublished.
    Jan · 1 years ago
    Thank you very much ! This works great.

    But how do you get the same function/rule to work for a range of cells, along an entire column, for example?

    I have a list of my business' contacts on different rows, with their contact details in different columns, and I want a add a column that registers and counts the number of times a given cell along each row is changed. The code you gave works great, but only for one cell at a time !

    I'm new to VBA, so I would greatly appreciate your support.

    I tried adding a range of cells into the code, so instead of "B9" and "C9", as given in the example above, I played around with variations like "B:B", "C:C", or "B9:B1000" and "C9:C1000", without any success.

    Thanks in advance,
    • To post as a guest, your comment is unpublished.
      crystal · 11 months ago
      Hi Jan,
      Please try the below VBA code. Hope it can help. Thanks for your comment.

      Private Sub Worksheet_Change(ByVal Target As Range)
      Dim xRg As Range, xCell As Range
      Dim xSRg, xRRg As Range
      Dim xFNum As Long

      Set xSRg = Range("B9:B1000")
      Set xRRg = Range("C9:C1000")

      Application.EnableEvents = False
      On Error Resume Next
      For xFNum = 1 To xSRg.count
      If Target = xSRg.Item(xFNum) Then
      xRRg.Item(xFNum).Value = xRRg.Item(xFNum).Value + 1
      Application.EnableEvents = True
      Exit Sub
      End If
      Next xFNum
      Application.EnableEvents = True
      End Sub
      • To post as a guest, your comment is unpublished.
        Jonathan · 10 months ago
        Hi Crystal,

        This is brilliant, on my matrix I have used this on one of the columns but I have struggled to duplicate this across multiple columns. Do you have a solution?

        Thanks in Advance