简体   繁体   English

使用RowSource属性填充Excel VBA中的组合框

[英]Using RowSource Property to Fill ComboBox in Excel VBA

I am trying to fill a ComboBox with selections equal to the first column of a data table, and keep getting 我试图用等于数据表第一列的选择填充ComboBox,并不断获取

Run-Time error 380 运行时错误380

I suppose I could use a loop and AddItem statements, but since I expect this to get large (several hundred entries), would prefer to stay away from loops that might slow down the execution. 我想我可以使用loop和AddItem语句,但是由于我希望它会变大(几百个条目),因此宁愿远离可能减慢执行速度的循环。 Any thoughts on what I am doing wrong? 对我在做什么错有任何想法吗?

Private Sub ClientNameComboxAdd_Enter()
Dim sht As Worksheet
Dim LastRow As Long, UseRow As Long
Dim NameFind As Range
Set sht = ThisWorkbook.Worksheets("Client Info")
LastRow = sht.ListObjects("Clients").Range.Rows.Count
If LastRow = 2 And sht.ListObjects("Clients").DataBodyRange(1, 1) = "" Then
    MsgBox "Can't Add a New Plan With No Clients Entered"
    Exit Sub
End If
ClientNameComboxAdd.RowSource = sht.ListObjects("Clients").ListColumns(1).DataBodyRange
End Sub

The problem is in the next-to-last line - ClientNameComboxAdd.RowSource ... The first part, which comes into play before clients are added to the table, seems to work fine. 问题出在倒数第二行-ClientNameComboxAdd.RowSource ...第一部分,在将客户端添加到表之前起作用了,似乎工作正常。

Any thoughts would be appreciated. 任何想法将不胜感激。

I did it this way. 我是这样做的。 No VBA code at all. 完全没有VBA代码。 Just Excel 100%. 只是Excel 100%。

First, I created a Table named "T_DATA" with 2 columns, COUNTRY and CONTINENT, and after that I added 3 records: 首先,我创建了一个名为“ T_DATA”的表,其中包含2列COUNTRY和CONTINENT,然后添加了3条记录:

    Country         Continent
United States        America
Spain                 Europa
Germany               Europa

After that, I created a Range named RANGECOMBOBOX, and source is the Country column of my table. 之后,我创建了一个名为RANGECOMBOBOX的范围,其来源是表的“国家/地区”列。 Check image: 查看图片:

在此处输入图片说明

You have to make sure this named range refers to the column of your table. 您必须确保此命名范围引用表的列。 In my case is just "=T_DATA[Country]". 就我而言,只是“ = T_DATA [国家]”。

After that, I Inserted the combobox and Right click on it, tab CONTROL, and I set the Entry Range as the named range I created before, in my case, RANGECOMBOBOX: 之后,我插入了组合框并右键单击它,在CONTROL选项卡上,将“输入范围”设置为我之前创建的命名范围,在我的情况下是RANGECOMBOBOX:

在此处输入图片说明

Third step is testing if the combobox get the values of the Country Column, and it does: 第三步是测试组合框是否获取“国家/地区”列的值,并且可以:

在此处输入图片说明

Now final test, I inserted 2 new records in my table (China and Japan values) and the combobox updates instantly, by just adding records to my table. 现在是最终测试,我在表中插入了2条新记录(中国和日本值),并且通过将记录添加到表中,组合框立即更新。

在此处输入图片说明

Hope this can give you a light in your task. 希望这可以帮助您轻松完成任务。 Another option you have is doing the same I did but activate first the macro recorder and check the code. 您可以执行的其他选择与我相同,但首先激活宏记录器并检查代码。 Maybe it can help you out with your issue. 也许可以帮助您解决问题。

UPDATED ANSWER: 更新的答案:

Then try 然后尝试

Private Sub UserForm_Initialize()
Me.ClientNameComboxAdd.RowSource = "RangeCombobox"
End Sub

This worked on my userform, and the combobox loaded the countries. 这在我的用户表单上起作用,并且组合框加载了国家/地区。 However, remember to unload and load again your form every time you insert new records into your table, to update the combobox 但是,请记住,每次在表中插入新记录时都要卸载并再次加载表单,以更新组合框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM