Excel ActiveX Combobox shows previous selection when losing focus

↘锁芯ラ 提交于 2019-12-13 02:49:18

问题


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

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:

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

回答1:


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.

Like the following:

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


来源:https://stackoverflow.com/questions/47765242/excel-activex-combobox-shows-previous-selection-when-losing-focus

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