简体   繁体   English

VBA Excel 用户窗体组合框填充/添加

[英]VBA Excel UserForm ComboBox Populate/Add

I have a dynamic Name range that I want to populate a UserForm ComboBox with.我有一个动态名称范围,我想用它来填充 UserForm ComboBox

Range1=OFFSET(Sheet1!$A$1,1,0,COUNTA(Sheet1!$A:$A)-1,1)

How do I populate the combobox with this range and use the combobox to add and sort the range upon entry?如何使用此范围填充combobox combobox并在输入时使用combobox添加和排序范围?

Disclaimer: I am not going to give you code on a platter but I can surely get you on the right track :)免责声明:我不会在盘子上给你代码,但我肯定能让你走上正轨:)

First things First第一件事

COUNTA(Sheet1!$A:$A)-1 is the wrong way to find the last row number. COUNTA(Sheet1!$A:$A)-1是查找最后一行编号的错误方法。 What if the Col A has blanks?如果 Col A 有空格怎么办?

Use =MAX((A:A<>"")*(ROW(A:A))) instead.使用=MAX((A:A<>"")*(ROW(A:A)))代替。 This is an array formula.这是一个数组公式。 Which means that you have to use CTRL + SHIFT + ENTER if you are using it directly in a worksheet.这意味着如果您直接在工作表中使用它,则必须使用CTRL + SHIFT + ENTER If you are using it in a named range then you do not have to worry about it.如果您在命名范围内使用它,那么您不必担心它。

在此处输入图片说明

Secondly其次

To loop through a range and add to a combobox, you can use this code要遍历范围并添加到组合框,您可以使用此代码

Dim aCell As Range

For Each aCell In MYRANGE
    Combobox1.AddItem aCell.Value
Next aCell

Thirdly第三

To add to a range, you can simply use要添加到范围,您只需使用

Range("A1").Value = Combobox1.value

Lastly最后

To sort a range对范围进行排序

'~~> Where ws is the relevant worksheet
ws.Columns("A:AE").Sort Key1:=ws.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

You can use the Combobox1 click event to perform the addition and sorting of the range可以使用Combobox1点击事件进行范围的添加和排序

Hope this gets you in the right track :)希望这能让你走上正轨:)

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

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