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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24219796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:26:55  来源:igfitidea点击:

My ComboBox doesn't display the values I've added in VBA

excelvbaexcel-vbacomboboxuserform

提问by Dubeddo

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

采纳答案by Dubeddo

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. Click on view code
  3. 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.
  4. In this module you may code everything you want about the content of the combobox.
  1. 右键单击用户表单
  2. 点击查看代码
  3. 在顶部,您将看到可以选择的两个类别,您应该选择“用户表单”和“激活”,完成此步骤后,excel 必须将新模块添加到您的代码中。
  4. 在此模块中,您可以编写有关组合框内容的所有内容。

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.

您还应该注意组合框的拼写,如果拼写错误,您可能无法看到组合框的内容。

回答by edsolutions

Ensure that the code segment you have posted is in the 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.

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