我的ComboBox不显示我在VBA中添加的值

时间:2022-01-29 05:52:19

I'm trying to add options to a combo box in a userform. When I run the code, Excel doesn't give any errors, however when the userform shows up it doesn't display the entities I have added to the combobox previously. That is, when I click on the combobox, it doesn't show any options, only one blank row, as if no items were added to it.

我正在尝试将选项添加到用户表单中的组合框。当我运行代码时,Excel不会给出任何错误,但是当用户窗体显示时,它不会显示我之前添加到组合框中的实体。也就是说,当我点击组合框时,它没有显示任何选项,只显示一个空白行,就像没有添加任何项目一样。

Here is the code I'm using:

这是我正在使用的代码:

Private Sub UserForm_Initialize()
    ComboBox1.AddItem "xxx"
    ComboBox1.AddItem "yyy"
    ComboBox1.AddItem "zzz"
End Sub

I am using the following code to call the user form within a macro:

我使用以下代码在宏中调用用户表单:

UserForm.Show

2 个解决方案

#1


2  

The code given in the question works perfectly well. In my case the code didn't work because I manually entered this part of the code into VBA:

问题中给出的代码非常有效。在我的情况下代码不起作用,因为我手动将这部分代码输入VBA:

Private Sub UserForm_Initialize()

If you make Excel create this module for you instead of writing it on your own, your code should work perfectly. Excel did not have "Initialize" as a default form so I tried "Activate" and it worked.

如果您让Excel为您创建此模块而不是自己编写,那么您的代码应该可以正常工作。 Excel没有“初始化”作为默认表单,所以我尝试“激活”,它工作。

To create this module you have to do the following steps:

要创建此模块,您必须执行以下步骤:

  1. Right click on user form
  2. 右键单击用户表单
  3. Click on view code
  4. 单击视图代码
  5. On top you will see two categories you can pick, you should pick "Userform" and "Activate", after completing this step excel must add a new module to your code.
  6. 在最上面你会看到两个你可以选择的类别,你应该选择“Userform”和“Activate”,完成这一步后,excel必须在你的代码中添加一个新模块。
  7. In this module you may code everything you want about the content of the combobox.
  8. 在本单元中,您可以编写有关组合框内容的所有内容。

You should also be careful with the spelling of your combobox, if you spell it incorrectly, you may be unable to see the contents of the combobox.

您还应该小心组合框的拼写,如果拼写不正确,您可能无法看到组合框的内容。

#2


0  

Ensure that the code segment you have posted is in the userform.

确保您发布的代码段位于userform中。

Right click on the user form in the VBA view and choose "View Code". Is this where the code is?

右键单击VBA视图中的用户表单,然后选择“查看代码”。这是代码的位置吗?

Are you sure that the User Form is called 'UserForm' and not 'UserForm1'? 'UserForm1' is the default, similar to 'ComboBox1'.

您确定用户表单被称为“UserForm”而不是“UserForm1”吗? 'UserForm1'是默认值,类似于'ComboBox1'。

The below works for me.

以下适用于我。

'in the UserForm1 code
Private Sub UserForm_Initialize()
ComboBox1.AddItem "xxx"
ComboBox1.AddItem "yyy"
ComboBox1.AddItem "zzz"
End Sub

The below will display the form.

以下将显示表格。

UserForm1.Show

Is this the only form in the workbook? Create a new one and see if it does the same thing.

这是工作簿中唯一的形式吗?创建一个新的,看它是否做同样的事情。

#1


2  

The code given in the question works perfectly well. In my case the code didn't work because I manually entered this part of the code into VBA:

问题中给出的代码非常有效。在我的情况下代码不起作用,因为我手动将这部分代码输入VBA:

Private Sub UserForm_Initialize()

If you make Excel create this module for you instead of writing it on your own, your code should work perfectly. Excel did not have "Initialize" as a default form so I tried "Activate" and it worked.

如果您让Excel为您创建此模块而不是自己编写,那么您的代码应该可以正常工作。 Excel没有“初始化”作为默认表单,所以我尝试“激活”,它工作。

To create this module you have to do the following steps:

要创建此模块,您必须执行以下步骤:

  1. Right click on user form
  2. 右键单击用户表单
  3. Click on view code
  4. 单击视图代码
  5. On top you will see two categories you can pick, you should pick "Userform" and "Activate", after completing this step excel must add a new module to your code.
  6. 在最上面你会看到两个你可以选择的类别,你应该选择“Userform”和“Activate”,完成这一步后,excel必须在你的代码中添加一个新模块。
  7. In this module you may code everything you want about the content of the combobox.
  8. 在本单元中,您可以编写有关组合框内容的所有内容。

You should also be careful with the spelling of your combobox, if you spell it incorrectly, you may be unable to see the contents of the combobox.

您还应该小心组合框的拼写,如果拼写不正确,您可能无法看到组合框的内容。

#2


0  

Ensure that the code segment you have posted is in the userform.

确保您发布的代码段位于userform中。

Right click on the user form in the VBA view and choose "View Code". Is this where the code is?

右键单击VBA视图中的用户表单,然后选择“查看代码”。这是代码的位置吗?

Are you sure that the User Form is called 'UserForm' and not 'UserForm1'? 'UserForm1' is the default, similar to 'ComboBox1'.

您确定用户表单被称为“UserForm”而不是“UserForm1”吗? 'UserForm1'是默认值,类似于'ComboBox1'。

The below works for me.

以下适用于我。

'in the UserForm1 code
Private Sub UserForm_Initialize()
ComboBox1.AddItem "xxx"
ComboBox1.AddItem "yyy"
ComboBox1.AddItem "zzz"
End Sub

The below will display the form.

以下将显示表格。

UserForm1.Show

Is this the only form in the workbook? Create a new one and see if it does the same thing.

这是工作簿中唯一的形式吗?创建一个新的,看它是否做同样的事情。