记录单元历史记录,如果记录超过N行,则停止记录单元历史

时间:2018-02-14 21:03:57

标签: excel vba cell history

到目前为止,我已经将单元格历史记录写入新行所需的代码,但是一旦收集了所需数量的值,我就错过了停止写入单元格历史记录所需的代码。我希望宏一旦存储了100或1000个值,就停止记录单元格历史记录

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Dim sDate As Worksheet
Dim nNewRow As Long

Set sData = Worksheets("sheet1")
nNewRow = sData.Cells(Rows.Count, 1).End(xlUp).Row + 1
sData.Cells(nNewRow, 1).Value = sData.Range("A1").Value

End Sub

1 个答案:

答案 0 :(得分:0)

这样的事情对你有用。请注意,您不必定义工作表,传递的Sh对象变量会为您执行此操作,只需指定要检查的工作表。

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)

    Dim nNewRow As Long

    'Disable events to prevent infinite loops
    Application.enableevents = False

    'Verify we are working with Sheet1
    If LCase(Sh.Name) = "sheet1" Then
        'Check the value of nNewRow
        nNewRow = Sh.Cells(Sh.Rows.Count, 1).End(xlUp).Row + 1
        If nNewRow <= 10 Then
            'Within threshold, record value
            Sh.Cells(nNewRow, 1).Value = Sh.Range("A1").Value
        End If
    End If

    'Turn events back on
    Application.enableevents = True

End Sub