VBA ListBox - 所选值返回空白 [英] VBA ListBox - Selected Value is Returning a Blank

查看:61
本文介绍了VBA ListBox - 所选值返回空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些单选按钮和两个单列列表框的用户窗体.在某些情况下,我无法从第二个 ListBox 返回值.

I have a UserForm with some radio buttons and two single column ListBox's. I'm having trouble returning the value from the second ListBox under certain circumstances.

如果用户选择其中一个单选按钮,他们会在第一个 ListBox 中获得一系列可供选择的项目.一旦他们从第一个 ListBox 中选择,第二个 ListBox 就会填充项目.

If the user selects one of the radio buttons they get a series of items to select from in the first ListBox. Once they select from the first ListBox, the 2nd ListBox gets populated with items.

如果他们选择其他单选按钮,则两个 ListBox 只会填充一个值不适用";我会立即选择它.

If they select the other radio buttons, the two ListBox's just get populated with a single value "Not Applicable" and I'm selecting it straight away.

这是我用来将两个 ListBox 设置为不适用"的代码

This is the code I'm using to set the two ListBox's up with "Not Applicable"

ListBox_First.Clear
ListBox_Second.Clear
ListBox_First.List = Array("Not Applicable")
ListBox_First.Selected(0) = True
ListBox_Second.List = Array("Not Applicable")
ListBox_Second.Selected(0) = True

这是我获取选定值的代码

This is my code to get the value selected

Dim firstValue As String
Dim secondValue As String
firstValue = ListBox_First.Value
secondValue = ListBox_Second.Value

firstValue 可以,因为它等于Not Applicable",但是 secondValue 等于".当您查看表单时,每个 ListBox 中的值看起来都已被选中,所以我不明白为什么它是空白的.我检查了 ListCount 属性,每个 ListBox 只有一个项目,所以应该正确选择它.

firstValue is ok as it equals "Not Applicable", however secondValue is equal to "". When you look at the form, the value in each ListBox looks like it's selected so I don't understand why it's blank. I have checked the ListCount property and each ListBox only has one item so it should be correctly selected.

如果我手动选择不适用"在第二个 ListBox 中使用鼠标工作正常,但我试图避免用户在它是唯一值时必须选择它.

If I manually select "Not Applicable" in the second ListBox using the mouse is works fine, but I'm trying to avoid the user having to select it when it's the only value.

我不知道这是错误还是我的代码有问题.

I don't know if this is a bug or if I've done something wrong with my code.

我正在使用一个名为 WRQ Reflections 的产品.

I'm working with a product called WRQ Reflections.

推荐答案

通过添加 setfocus 命令识别该值.

The value is recognized by adding the setfocus command.

firstValue = ListBox_First.Value
ListBox_Second.SetFocus
secondValue = ListBox_Second.Value

这篇关于VBA ListBox - 所选值返回空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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