我正在尝试使用VBA更改Excel 2015中状态栏中显示的文本。但是,它似乎没有更新。消息“Ready”仍然存在。
Function testStatusBar(delay As Long)
Dim progress As Long
For progress = 1 To delay
Application.StatusBar = "Progress: test ongoing ...." & (progress / delay) & "%"
DoEvents
Next progress
Application.StatusBar = False
End Function
我也尝试了许多答案,这些答案在本网站上给出了类似的问题,但没有任何效果。代码有问题或我是否需要更改某些设置?
答案 0 :(得分:1)
您的功能正常,但我怀疑您是从Excel单元格中将其称为UDF。不允许UDF更新状态栏,也不允许对Excel环境进行任何其他更改。 (基本上,允许UDF返回一个值,就是这样。)
如果您从一段VBA代码调用该函数,它可以正常工作,只要您将其更改为返回如下值:
Function testStatusBar(delay As Long) As String
Dim progress As Long
For progress = 1 To delay
Application.StatusBar = "Progress: test ongoing ...." & (progress / delay) & "%"
DoEvents
Next progress
Application.StatusBar = False
testStatusBar = "Finished"
End Function
(然后您可以通过说myReturnValue = testStatusBar(200000)
或者将其更改为Sub而不是Function,即
Sub testStatusBar(delay As Long)
Dim progress As Long
For progress = 1 To delay
Application.StatusBar = "Progress: test ongoing ...." & (progress / delay) & "%"
DoEvents
Next progress
Application.StatusBar = False
End Sub
(然后通过说testStatusBar 200000
来调用它。)