【问题标题】:ListBox and ComboBox are always emptyListBox 和 ComboBox 始终为空
【发布时间】:2021-09-20 05:22:29
【问题描述】:

我在组合和列表框的 UserForm_Initialize() 子例程下有以下代码。当我显示用户窗体时,两个框都没有添加任何项目,即使我使用 With 语句添加它们是为了方便。

Private Sub MoneySpinButton_Change()
MoneyTextBox.Text = MoneySpinButton.Value
End Sub
Private Sub ClearButton_Click()
Call DinnerPlanner_Initialize
End Sub
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub OkButton_Click()

Dim emptyRow As Long

'Make Sheet1 active
Sheet1.Activate

'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Transfer Information
Cells(emptyRow, 1).Value = NameTextBox.Value
Cells(emptyRow, 2).Value = PhoneTextBox.Value
Cells(emptyRow, 3).Value = CityListBox.Value
Cells(emptyRow, 4).Value = DinnerComboBox.Value

If DateCheckBox1.Value = True Then Cells(emptyRow, 5).Value = DateCheckBox1.Caption
If DateCheckBox2.Value = True Then Cells(emptyRow, 5).Value = DateCheckBox2.Caption
If DateCheckBox1.Value = True Then Cells(emptyRow, 5).Value = DateCheckBox3.Caption

If CarOptionButton2.Value = True Then
    Cells(emptyRow, 6).Value = "Yes"
Else
    Cells(emptyRow, 6).Value = "No"
    
End If
Cells(emptyRow, 7).Value = MoneyTextBox.Value

End Sub
Private Sub DinnerPlanner_Initialize()
'Fix Issues with size changes
With DinnerPlanner
.Width = 369.75
.Height = 538.5
End With

'Empty NameTextBox
NameTextBox = ""

'Empty PhoneTextBox
PhoneTextBox = ""

'Empty CityListBox
CityListBox.Clear

'Fill CityListBox
With Me.CityListBox
    .AddItem "San Francisco"
    .AddItem "Oakland"
    .AddItem "Richmond"
End With

'Empty DinnerComboBox
DinnerComboBox.Clear

'Fill DinnerComboBox
With Me.DinnerComboBox
    .AddItem "Italian"
    .AddItem "Chinese"
    .AddItem "Fries and Meat"
End With

'Uncheck Date Check Boxes
DateCheckBox1.Value = False
DateCheckBox2.Value = False
DateCheckBox3.Value = False

'Set no car as default
CarOptionButton2.Value = True

'Empty MoneyTextBox
MoneyTextBox.Value = ""

'Set Focus on NmaeTextBox
NameTextBox.SetFocus

End Sub

为什么列表框和组合框总是空的。 AddItem & Items.Add 的方法在哪里试过,但都没有显示项目?

【问题讨论】:

  • 应该是.AddItem 而不是.Items.Add
  • 另外,去掉括号
  • @Simon @braX 嗨,我已经尝试过 .AddItem 方法,我删除了括号。这是同一件事。这就是我在上面测试代码的原因。
  • 请显示初始化事件的完整代码,因为没有理由它不应该工作。您在项目周围留下引号以添加正确吗?您也可以通过在表单上放置一个按钮进行测试,然后将您的代码放在上面,仅在 click 事件上,看看它是否真的有效。
  • @Simon 我已将所有 VBA 代码放在上面。唯一剩下的是分配给按钮以显示用户窗体的宏-我已将宏放在模块代码中。

标签: vba combobox listbox


【解决方案1】:

试试这个方法。它对我有用。 没有 Dinner_Initialize(),因为 Dinner 不是 UserForm 对象。 (这里的UserForm 和DinnerForm 或UserForm1 不一样,这是通用对象)。 My result

Public Sub UserForm_Initialize()
'Fix Issues with size changes
Width = 369.75
Height = 538.5

'Empty NameTextBox
NameTextBox = ""

'Empty PhoneTextBox
PhoneTextBox = ""

'Empty CityListBox
CityListBox.Clear

'Fill CityListBox
With CityListBox
    .AddItem "San Francisco"
    .AddItem "Oakland"
    .AddItem "Richmond"
End With

'Empty DinnerComboBox
DinnerCombobox.Clear

'Fill DinnerComboBox
With DinnerCombobox
    .List = Array("Italian", "Chinese", "Fries and Meat") 'your items
    .ListIndex = 0            'default value
    .Style = fmStyleDropDownList 'type of input
End With


'Uncheck Date Check Boxes
DateCheckBox1.Value = False
DateCheckBox2.Value = False
DateCheckBox3.Value = False

'Set no car as default
CarOptionButton2.Value = True

'Empty MoneyTextBox
MoneyTextBox.Value = ""

'Set Focus on NmaeTextBox
NameTextBox.SetFocus

End Sub

【讨论】:

  • 我尝试了 '.List' 属性和 'Array' 而不是 '.AddItems' 方法,但没有任何变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
  • 2017-07-31
  • 2019-07-18
  • 2019-08-18
相关资源
最近更新 更多