Excel列表框listindex不会突出显示

可以有一个更好的水平的Excel能力请帮助我下面的代码? 我已经在Worksheet_Activate事件中进行了设置。 我有代码将ActiveX列表框设置为默认值,如下所示。 无论出于何种原因,列表框不显示作为高亮项目的默认值。 所有其他的逻辑似乎工作正常,但它让我疯狂,下面的代码将不会突出我的第一个列表框中愚蠢的第一个项目。 我究竟做错了什么?

With CTOverview.ListBox1 .IntegralHeight = True .Height = 114.75 .Width = 125.25 .IntegralHeight = False .ListIndex = 0 .Selected(0) = True .Value = "Entire Division" End With CTData.Range("Overview_RegionSelected").Value = CTOverview.ListBox1.Value With CTOverview.ListBox2 .IntegralHeight = True .Height = 114.75 .Width = 150 .IntegralHeight = False .ListIndex = -1 End With 

谢谢你的帮助。

我知道。 过去我已经看到这种奇怪的行为,绝对没有任何解释。 有时它可以工作,有时不会。 尝试这个。 这将工作。

 With CTOverview.ListBox1 If .ListCount > 1 Then .Selected(1) = True .IntegralHeight = True .Height = 114.75 .Width = 125.25 .IntegralHeight = False .ListIndex = 0 .Selected(0) = True .Value = "Entire Division" End With 

跟进

这是比我早先给出的代码更好的代码。 上述代码仅限于我们需要超过1个Listcount 。 以下将为1个Listcount工作。

 Dim rng As Range, aCell As Range With CTOverview.ListBox1 .IntegralHeight = True .Height = 114.75 .Width = 125.25 .IntegralHeight = False .ListIndex = 0 .Selected(0) = True .Value = "Entire Division" Set rng = Range(.ListFillRange) For Each aCell In rng aCell.Formula = aCell.Formula Next End With 

简化你的代码。 使用.Selected属性或.ListIndex属性或.Value属性来select列表中的项目。

在您的第一个列表框(ListBox1)中,您已经使用了所有三个属性来使您的默认select生效。 可能你的代码中的值不一致,如列表中的第一项不是“整个部门”。 如果您的第一个项目只是“全部分区”,那么请使用任何一个属性来实现所需的select。

在第二个列表框(ListBox2)中,当您的代码具有.ListIndex = -1时,这意味着您要取消select所有内容。 因此,将所需的列表索引更改为0以select第一个项目。

鉴于你想要默认select列表框的第一个选项,请将以下内容作为基础。

 With CTOverview.ListBox1 .ListIndex = 0 End With With CTOverview.ListBox2 .ListIndex = 0 End With 

让我知道你的想法。

所以我有同样的问题,我提到了Office文档,并意识到一切都依赖于列表框应该在焦点,以使其值属性返回值的前提。

我build议你只要在最后添加一行来引起关注。

 With CTOverview.ListBox1 .IntegralHeight = True .Height = 114.75 .Width = 125.25 .IntegralHeight = False .ListIndex = 0 .Selected(0) = True .Value = "Entire Division" .SetFocus End With 

我已经看到与用户窗体中的列表框控件(不embedded工作表中的ActiveX控件)类似的问题,但由于我还没有发现任何其他post出现在第一个谷歌search页面,我附上我的解决scheme这一个,因为它很可能是狩猎UserForm解决scheme的人会在这个post上,再加上它可能也有助于一些正在使用直接ActiveX ListBox控件。

在UserForms(至less)设置.ListIndex = n和.Selected(n)= True似乎没有任何帮助。 我find的唯一解决方法是这样的,其中“n”是要select/突出显示的列表项:

 Dim SaveVal As String With MyUserForm.SomeListBox SaveVal = .List(n) .Value = "" .Value = SaveVal .SetFocus 'Not required, but allows up/down arrow keys to function normally. End With 

请注意,只是将所选列表项重新分配给其当前值不起作用,因为底层代码显然检查该情况并将其视为无操作。