问题
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