简体   繁体   English

VBA停止在Excel中打印值

[英]VBA stops printing values in excel

I have written a macro to take values from an external simulation tool and prints the results in excel.The simulaion tool will give values only every 30 seconds and will run for days. 我编写了一个宏以从外部仿真工具中获取值并在excel中打印结果。仿真工具仅每30秒给出一次值,并且可以运行数天。 Hence i have given a delay in VB for 30 seconds in loop. 因此,我给了VB 30秒的循环延迟。 I leave it overnight for running. 我把它过夜运行。 In the next morning i could see none of the results were updated after certain rows.But the VBA editor header shows the macro is running and the external simulation tool is also running. 第二天早上,在某些行之后,我看不到任何结果被更新。但是VBA编辑器标头显示了宏正在运行,并且外部仿真工具也正在运行。 The last updated row in excel is not constant everytime. excel中最后更新的行并非每次都是恒定的。 Why does VBA stops printing the results in excel? 为什么VBA停止在excel中打印结果? Any help will be much appreciated. 任何帮助都感激不尽。

Sample code: 样例代码:

For l = 3 To lastrow1_mul + 2
Module4.Wait 30
nv = send_data.Call
Sheets(SecondSheetName).Range(SecondSheetCol_9 & l) = Hex(nv)
dv = DT.Call
If dv = 44 Then
Sheets(SecondSheetName).Range(SecondSheetCol_10 & l) = "A"
ElseIf dv = 54 Then
Sheets(SecondSheetName).Range(SecondSheetCol_10 & l) = "B"
Else
Sheets(SecondSheetName).Range(SecondSheetCol_10 & l) = "C"
End If
Next l

Module 4: 单元4:

Function Wait(PauseTime As Single)
Dim StartTime As Single 
StartTime = Timer
While Timer < StartTime + PauseTime
DoEvents
Wend  
End Function

Send_data and DT are external simulation tool's functions.Variable lastrow1_mul's value is getting updated around 7000 but rows in excel stops printing around 500 itself(not constant always) . Send_data和DT是外部仿真工具的功能。变量lastrow1_mul的值将在7000左右更新,但是excel中的行将停止打印500左右(并非始终恒定)。

looks OK but look at using Application.OnTime shown below. 看起来不错,但请使用下面显示的Application.OnTime。 Also what happens if the simulation hasnt returned data, shoudnt you build in a bigger, maybe 45 second intervals? 如果仿真还没有返回数据,应该以更大的间隔(也许45秒)构建,该怎么办?

Application.OnTime Now + TimeValue("00:00:30"), "RunProcess"

The 'Timer' i used in the code was the culprit. 我在代码中使用的“计时器”是罪魁祸首。 Timer counts the seconds elapsed(in fractions) since midnight. 计时器计数自午夜以来经过的秒数(以分数为单位)。 So it will not update when the time becomes 00.00.00. 因此,当时间变为00.00.00时,它将不会更新。 So the wait function i wrote will keep on waiting! 因此,我编写的等待函数将继续等待! Here is the solution 这是解决方案

Function Wait()
Dim StartSecond As Single
Dim EndSecond
StartSecond = Now

EndSecond = DateAdd("s", 30, Now)

While Now < EndSecond
DoEvents
Wend

End Function

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

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