简体   繁体   English

在从Excel导出到CSV文件的数据中实施延迟

[英]Implementing delay in data export from Excel to csv file

I have data comming into Excel from a DDE application with a macro. 我有一个带有宏的数据从DDE应用程序传入Excel。 I managed to program the macro so that each data in stored in the next row. 我设法对宏进行编程,以便将每个数据存储在下一行中。 Problem: I want to be able to scroll through the data eg to view te values of 5 minutes ago, but I can't because to macro is blocking the workbook. 问题:我希望能够滚动浏览数据,例如查看5分钟前的te值,但是我不能,因为to宏阻塞了工作簿。 I tried to export the data to a csv file, and view it form there, but I have a problem with the loop saying the file is already open. 我试图将数据导出到一个csv文件,并在那里查看它的形式,但是循环有问题,说该文件已经打开。 Anyone have any solution? 有人有什么解决办法吗? I'm a real beginner in VBA or programming. 我是VBA或编程方面的真正初学者。 Thanks. 谢谢。 Here's what i've been trying to run: 这是我一直试图运行的:

Sub DDEread()

' Opens a DDE conversation with the Windmill DDE
' Panel using the Service Name "Windmill" and the
' Topic Name "data"
Dim i As Integer, g As Integer
For i = 2 To 15
' Loop to get 5 values, 1 every second
For g = 0 To 4
ddechan = Excel.DDEInitiate("Windmill", "data")
Cells(g + 1, 4).Value = Excel.DDERequest(ddechan, "Ch0")
Cells(g + 1, 5).Value = Excel.DDERequest(ddechan, "00001")
Application.Wait Now + TimeValue("00:00:01")
Next g

'put the calculated average of 5 cells into other cells to get allways 5 second averaged values
Cells(i, 2).Value = Cells(6, 4)
Cells(i, 3).Value = Cells(6, 5)
Cells(i, 1).Show
Cells(i, 1).Value = Format(Time, "hh:mm:ss")
Cells(i, 1).NumberFormat = "hh:mm:ss"
' Closes the DDE conversation.
Excel.DDETerminate (ddechan)

'Data export to csv file:
Dim myFile As String, cellValue As Variant
myFile = Application.DefaultFilePath & "\PG350data.csv"
Open myFile For Output As #1

cellValue = Cells(i, 2).Value + Cells(i, 3).Value
Write #1, cellValue


Next i
Close #1
End Sub

Refer to the following example of using DoEvents demonstrating how to add responsiveness to the program during execution of a lengthy loop: 请参考以下使用DoEvents示例,该示例演示如何在执行冗长的循环期间向程序添加响应性:

   Open "com1" For Input As #1
   Input #1, x
   Do Until x = Chr(13)
   DoEvents
   '...
   '...
   Input #1, x
   Loop

Another example: 另一个例子:

   X = Timer()
   Do While X + 10 > Timer()

       DoEvents

   Loop

Detailed explanation here: http://support.microsoft.com/kb/118468 此处的详细说明: http : //support.microsoft.com/kb/118468

Hope this will help. 希望这会有所帮助。

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

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