Excel live data counter (DDE Server)

▼魔方 西西 提交于 2021-01-29 12:14:46

问题


I'm using DDE server to import live data on excel on two columns, the data changes every second.

What im currently doing is i have live data on Cell A1 and live data on Cell B1 on Cell C1 i have the following formula

=A1=B1

Which is suppose to be true, but sometimes the result is false (as cell A1 doesn't match cell B1)

I want to calculate how many times "false" is the result in C1

My problem is its a live data and changes almost every second, and my result should be accumulated. Does anyone have any suggestion on how can it be done on excel?

Thanks,


回答1:


Give this a try :

Private Sub Worksheet_Calculate()

Dim Count As Long
Dim KeyCells As Range

Set KeyCells = Range("C1")

Count = 0

If IsEmpty(Range("D1").Value) = False Then
    Count = Range("D1").Value
End If


    If KeyCells.Value = False Then
        Count = Count + 1
        Range("D1").Value = Count
    End If

End Sub

Note : Here D1 Cells will Save the counted value, Change the cells if needed ... This code will count how much time "False" are returned ..

Credit to tim for worksheet_calculate()




回答2:


What you need is the Worksheet_Calculate event. Each time a calculation occurs, this event is triggered.

Private Sub Worksheet_Calculate()
Dim i As Long

'Check if cell C3's value is FALSE
If Cells(1, 3) = False Then
    'Loop through all customer properties of the workbook
    For i = 1 To CustomProperties.Count
        'Check if there is a property named Counter
        If CustomProperties.Item(i).Name = "Counter" Then
            'If there is, add 1 to the property's value and exit the sub
            CustomProperties.Item(i).Value = CustomProperties.Item(i).Value + 1
            Exit Sub
        End If
    Next i

    'If the customer property was not found, 
    'add it and add the value 1 to it
    CustomProperties.Add Name:="Counter", Value:=1
End If

End Sub

This code adds a custom property to the workbook file. This removes clutter from the worksheet by storing the counter in the file data instead.

The counter's value can be checked with CustomProperties.Item(1).Value if it's the first property in the file (which is likely, unless you have added more)



来源:https://stackoverflow.com/questions/57672519/excel-live-data-counter-dde-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!