Excel ActiveX组合框失去焦点时显示先前的选择 [英] Excel ActiveX Combobox shows previous selection when losing focus

查看:129
本文介绍了Excel ActiveX组合框失去焦点时显示先前的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,用Sheet2上的Table1的Name列填充Sheet1上的组合框。

I have this code which fills a combobox on Sheet1 with the Name column of Table1 on Sheet2.

Public Sub Worksheet_Activate()
   Me.ComboBox1.List = Worksheets("Sheet2").ListObjects("Table1")_
   .ListColumns("Name").DataBodyRange.Value
End Sub

效果很好,但是当我在表格上单击组合框时,它的效果很奇怪。框中的选定条目会快速闪烁到上一个条目。例如,当前选择的项目是 b,然后我选择 c。如果单击工作表,则框中的条目会快速闪烁为 b,然后再返回 c。

Works fine but it has a weird effect when I click off the combobox onto the sheet. The selected entry in the box quickly flashes to the previous entry. For example, the currently selected item is "b" and then I select "c". If I click on the worksheet the entry in the box quickly flashes to "b" before going back to "c".

我已经将此代码单独放在了一个新文件中,但仍然得到了相同的效果。有人看到过吗?

I've put this code alone in a new file and I still get the same effect. Has anyone else seen this?

编辑有关公开订阅的原因:

Edit regarding reason for Public Sub:

忘记包含 Workbook_Open 代码,以便在打开工作簿时将Sheet1视为已激活。但是无论是否保留该代码都没关系,仍然可以看到效果。

Forgot to include the Workbook_Open code so that Sheet1 is considered Activated when you open the Workbook. But it doesn't matter if I keep that code or not, I still see the effect.

Private Sub Workbook_Open()
Call ActiveSheet.Worksheet_Activate
End Sub


推荐答案

在代码中添加一个 LostFocus 事件,该事件在工作表上选择了一个单元格,更改了ComboBox的值后,当您选择一个单元格时,闪烁不会发生。

Adding a LostFocus event with code that selects a cell on your worksheet should cause the flicker not to happen when you select a cell after changing the ComboBox's value.

类似于以下内容:

Private Sub ComboBox1_LostFocus()
    ActiveSheet.Range("A1").select
End Sub

这篇关于Excel ActiveX组合框失去焦点时显示先前的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆