简体   繁体   English

Excel ActiveX ComboBox 显示上一个值问题

[英]Excel ActiveX ComboBox Displays Previous Value Issue

Issue with Embedded ActiveX ComboBox form on a spreadsheet where:电子表格上嵌入式 ActiveX ComboBox 表单的问题,其中:

  • Upon changing the value in the ComboBox, the value changes properly更改 ComboBox 中的值后,值会正确更改
  • However, when a cell/shape is selected on the sheet, the ComboBox value reverts back to the previous value for a split second before going back to the new value但是,当在工作表上选择一个单元格/形状时,ComboBox 值会在一瞬间恢复为以前的值,然后再恢复到新值

  • Problem because: If a button for a macro is pressed after changing the ComboBox value, the old ComboBox value is displayed while the macro is running, rather than the new value问题原因:如果在更改 ComboBox 值后按下宏的按钮,则在宏运行时显示旧的 ComboBox 值,而不是新值

Question : Is there a way to force this event (reverting to old value before going displaying new value) programmatically?问题有没有办法以编程方式强制此事件(在显示新值之前恢复为旧值)?

I've tried using the following in the Change event for the ComboBox, as well as within the macro that is called by another shape on the sheet:我已经尝试在 ComboBox 的 Change 事件中以及在工作表上的另一个形状调用的宏中使用以下内容:

  • Calculating the worksheet计算工作表
  • Selecting/Activating a cell选择/激活单元格
  • ScreenUpdating = false, ScreenUpdating = true屏幕更新 = 假,屏幕更新 = 真

Did a bit more searching and have found this question Excel ActiveX Combobox shows previous selection when losing focus with the same issue进行了更多搜索并发现了这个问题Excel ActiveX Combobox在出现相同问题时失去焦点时会显示先前的选择

Will start from here:将从这里开始:

I created a comboBox with various fruits as items inside.我创建了一个组合框,里面有各种水果作为项目。

I clicked on pear.我点击了梨。 Then pineapple.然后是菠萝。 then clicked on cell A1, firing the lost focus event.然后单击单元格 A1,触发失去焦点事件。

Private Sub ComboBox1_LostFocus()
    Debug.Print ComboBox1.Value
    Debug.Print ComboBox1.Value
    Debug.Print ComboBox1.Value
End Sub

This was text in my immediate window: pineapple pineapple pineapple这是我直接窗口中的文字:菠萝菠萝菠萝

This makes me think that it is a rendering issue, not a value change, though maybe it is happening so fast that my debug.print doesn't catch it.这让我认为这是一个渲染问题,而不是值更改,尽管它可能发生得太快以至于我的 debug.print 没有捕捉到它。

Interestingly if you make the calls from inside VBA, the flash does not occur:有趣的是,如果您从 VBA 内部进行调用,则不会发生闪存:

Sub Main()
    ComboBox1.Value = "mango"
    Range("A1").Select
End Sub

Nor if you just run也不是你只是跑

    ComboBox1.Value = "mango"

then click on the worksheet and select a cell然后单击工作表并选择一个单元格

Added a LostFocus event to the ComboBox that had a line that selected a cell.LostFocus事件添加到具有选择单元格的行的组合框。 This removed the "flicker" to the previous value when another cell on the sheet was selected, and also caused the ComboBox value to "flicker" back to the new value after "flickering" to the old value when a command button was clicked after changing the ComboBox's value... This removed the "flicker" to the previous value when another cell on the sheet was selected, and also caused the ComboBox value to "flicker" back to the new value after "flickering" to the old value when a command button was clicked after changing ComboBox 的值...

Therefore this solved my issue (mostly -- was unable to prevent the "flicker" from happening upon running another macro via command button, but at least the value does not remain stuck at the old value while the other macro runs)因此,这解决了我的问题(主要是 - 无法防止在通过命令按钮运行另一个宏时发生“闪烁”,但至少在另一个宏运行时该值不会停留在旧值上)

Idea came from this thread: What event is triggered when user selects value from drop down ComboBox (ActiveX)?想法来自这个线程: 当用户从下拉组合框(ActiveX)中选择值时会触发什么事件?

I have one solution that is sort of a work around.我有一个可以解决的解决方案。

I inserted an ActiveX Label to the worksheet (on the same location as the ComboBox) and set the labels visible state to False .我将 ActiveX 标签插入到工作表(与 ComboBox 位于同一位置)并将标签visible状态设置为False

Everytime the drop-down box closes, the focus is set on the invisible label每次下拉框关闭时,焦点都设置在不可见标签上

First a global variable.首先是一个全局变量。

 Public DropDownBegin As Boolean

Then the sub-routine然后是子程序

Private Sub ComboBox1_DropButtonClick()
    DropDownBegin = Not DropDownBegin
    If Not DropDownBegin Then
        ActiveSheet.Shapes("Label1").OLEFormat.Object.TopLeftCell.Select
    End If
End Sub

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

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