Running an Excel macro when info is pushed through a DDE connection

我的梦境 提交于 2021-01-28 05:43:54

问题


I currently have an HMI that communicates with Excel through a DDE connection. If I update values in the HMI then the values are updated in the spreadsheet of Excel and vice versa. Which is exactly what I want. However, I created a VBA code that will also update values in the spreadsheet when values are changed in the HMI. This VBA code does not run though if I change the values through the HMI but will run if I manually change them in Excel. Is there something that I am not considering?

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Range("K1:K3"), Range(Target.Address)) Is Nothing Then
        Call UpdateDateandTime
    End If
End Sub

Sub UpdateDateandTime()
    MsgBox "yes"
End Sub

回答1:


as a crude workaround, If only three cells are involved, may create some simple formula in other cells involving the target cells (if not already exists) and use Worksheet_Calculate() event do the processing. Something like,

Public VarK1, VarK2, Vark3
Private Sub Worksheet_Calculate()
    If VarK1 <> Range("K1").Value Or VarK2 <> Range("K2").Value Or Vark3 <> Range("K3").Value Then
    VarK1 = Range("K1").Value
    VarK2 = Range("K2").Value
    Vark3 = Range("K3").Value
   'Call the required process
End If
End Sub

though other direct methods involving DDE may be also be available to do the same and may please research/wait further for more appropriate the commands. Another workaround way is to use Application.OnTime method.



来源:https://stackoverflow.com/questions/57014784/running-an-excel-macro-when-info-is-pushed-through-a-dde-connection

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