简体   繁体   中英

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. However you could just make your own dde function which calls your event code...

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" . Or in our case: "App|Topic!Item" which can be extracted from the string easily. However do note that DDE() is NOT a native function of excel . So it is unlikely we will be able to know how DDE() is working to make a clean solution.

Edit2

Given your comment, say DDE is streaming "12...13...14..." and you want to output B1 = 12 , B2=13 and B3=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. For this reason you may be forced into hooking into worksheet events instead of using the formula's calculate event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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