简体   繁体   English

Excel VBA-为DDE链接单元格中的每个更新值创建行

[英]Excel VBA - Create rows for each updated value in DDE link cell

可以说单元格A1有一个DDE链接“ = dde(realtime_stock_price)。每次更新时如何将值发送到列的新行?Change()函数不起作用,因为它不是用户进行更改。尝试了Calculate()函数,但不太确定如何实现它。

The formula "=dde()" isn't something I am familiar with. 我不熟悉公式“ = dde()”。 However you could just make your own dde function which calls your event code... 但是,您可以只创建自己的dde函数来调用事件代码...

Public Function dde2(ByVal app as string,ByVal topic as string,ByVal item as string) as variant
    with Application
        channelNumber = .DDEInitiate(app,topic) 
        dde2 = .DDERequest(channelNumber, "Topics")
        .DDETerminate(channelNumber)
    end with
    on error resume next   'incase macro doesn't exist
        Application.run "dde2_postExec", dde2
    on error goto 0
End Function

Sub dde2_postExec(ByVal message as string)
    'do stuff...
end sub

Edit 编辑

According to this question the string passed to =DDE() is "Service|Topic!Item" . 根据此问题 ,传递给=DDE()的字符串是"Service|Topic!Item" Or in our case: "App|Topic!Item" which can be extracted from the string easily. 或者在我们的示例中: "App|Topic!Item" ,可以轻松地从字符串中提取它。 However do note that DDE() is NOT a native function of excel . 但是请注意, DDE()不是excel的本机函数 So it is unlikely we will be able to know how DDE() is working to make a clean solution. 因此,我们不太可能知道DDE()如何工作以提供一个干净的解决方案。

Edit2 EDIT2

Given your comment, say DDE is streaming "12...13...14..." and you want to output B1 = 12 , B2=13 and B3=14 . 根据您的评论,假设DDE正在流式传输"12...13...14..."并且您想输出B1 = 12B2=13B3=14 Your code would be something like this [untested]: 您的代码将如下所示:

Sub dde2_postExec(ByVal message as string)
    dim v as variant: v=split(message,"...")
    for i = 1 to ubound(v)-1
        range("B" & i).value = v(i)
    next
end sub

However do note that this may not work if called as a formula. 但是请注意,如果将其作为公式调用,则可能无法正常工作。 The VBA runtime is designed such that when forumla's are evaluated they cannot write values to cells in the sheet, other than the cell that called them. VBA运行时的设计使得在评估forumla时,除了调用它们的单元格之外,它们无法将值写入工作表中的单元格。 For this reason you may be forced into hooking into worksheet events instead of using the formula's calculate event. 因此,您可能不得不使用工作表事件而不是使用公式的calculate事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM