簡體   English   中英

在從Excel導出到CSV文件的數據中實施延遲

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

我有一個帶有宏的數據從DDE應用程序傳入Excel。 我設法對宏進行編程,以便將每個數據存儲在下一行中。 問題:我希望能夠滾動瀏覽數據,例如查看5分鍾前的te值,但是我不能,因為to宏阻塞了工作簿。 我試圖將數據導出到一個csv文件,並在那里查看它的形式,但是循環有問題,說該文件已經打開。 有人有什么解決辦法嗎? 我是VBA或編程方面的真正初學者。 謝謝。 這是我一直試圖運行的:

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

請參考以下使用DoEvents示例,該示例演示如何在執行冗長的循環期間向程序添加響應性:

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

另一個例子:

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

       DoEvents

   Loop

此處的詳細說明: http : //support.microsoft.com/kb/118468

希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM