Updating caption on ActiveX button in Excel vba

这一生的挚爱 提交于 2020-01-07 05:11:12

问题


I'm trying to update the caption of an ActiveX control button on an Excel spreadsheet to note that the code associated with the click event is working. The code pulls data from an azure database so pressing the button triggers anywhere from a 5-10 sec delay.

My original code had a single sub that essentially looked like this:

Sub GetInfofromDB()
  btnUpdate.Caption = "Updating...."
  <<code to get data from azure db>>
  btnUpdate.Caption = "Update List" 'reverts button to standard text
End Sub

However the button caption never changes. The combobox update works.

I separated the caption update into its own sub and then called each sub in order when the button was pressed and got the same behavior, no button update.

If I call the sub with the caption update without calling the data update the button updates successfully.

So, each part works on its own but not when they happen serially. Ideas how to get my button to update prior to the data call?

thanks. mark


回答1:


Try adding a aaplication.screenupdating = true after changing the caption.

the below code worked for me. Use a DoEvents

Private Sub CommandButton1_Click()
CommandButton1.Caption = "true"
DoEvents
Application.ScreenUpdating = True

For i = 1 To 2
    Application.Wait (Now + TimeValue("0:00:1"))
Next i

CommandButton1.Caption = "False"
End Sub


来源:https://stackoverflow.com/questions/12738712/updating-caption-on-activex-button-in-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!