我有以下代码,当我逐行单步执行时(或者即使我只单步执行 1 或 2 次循环迭代然后触发其余部分),它的工作原理就像一个魅力。当我从一个按钮运行它时,代码不起作用,因为 Hyperion Retrieve 永远不会为我更改的部门的每次迭代更新。部门本身已正确更改(从 Excel 和生成的 PDF 文件中可以看出)。
因此,简而言之,代码工作时没有看到或捕获错误,但结果是一组具有相同数据的 PDF,尽管通过按下按钮运行时标有不同的部门。
我已经在网上搜索了很多,尝试使用DoEvents
但Application.Wait
没有成功。有没有人知道如何确保从按钮按下运行时每次循环迭代都会发生刷新?
Option Explicit
Declare Function HypMenuVRefresh Lib "HsAddin.dll" () As Long
Sub CreateAllPDFS()
'... setup code to declare variables and loop range ...
'loop through departments
Dim cel As Range
For Each cel In rngLoop 'rngLoop declared and set in setup code
'set department on drivers tab
wsDrivers.Range("B4").Value = "'" & cel.Value
'*** --> tried to wait before the loop (just shot in the dark type thing)
'Application.Wait (Now + TimeValue("00:00:02")) 'pauses for 10 seconds, adjust as needed
'refresh hyperion
Dim lngReturn As Long
lngReturn = HypMenuVRefresh()
' *** --> tried Do Events
'DoEvents
'*** --> tried to wait after the loop
'Application.Wait (Now + TimeValue("00:00:02")) 'pauses for 10 seconds, adjust as needed
'quick error check
If lngReturn <> 0 Then
MsgBox "Could Not Refresh!"
Exit Sub
End If
'save as pdf
wsPL.ExportAsFixedFormat xlTypePDF, cel.Offset(, 1) & "\" & cel.Offset(, 2) & ".pdf", , , , , , False
Next
End Sub