简体   繁体   English

如何在Excel VBA中创建通用组合框填充例程?

[英]How to create a generic combobox fill routine in Excel VBA?

I would like to have a generic method for filling combo boxes in Microsoft Visual Basic for Application 7.1. 我想有一个通用的方法来填充Microsoft Visual Basic for Application 7.1中的组合框。

I tried to cast a control parameter to comboBox with CType but Excel doesn't recognize this function. 我尝试使用CType将控制参数CType为comboBox,但Excel无法识别此函数。

How can I fill a comboBox in a generic way? 如何以通用方式填充comboBox?

The easiest way is through the name of the ComboBox. 最简单的方法是通过ComboBox的名称。 You do not have to cast a control to comboBox . 您不必将control comboBoxcomboBox

Public Sub FillComboBox(UserForm As UserForm, cbName As String, column As String, startRow As Integer)
    Dim dataSheet As Worksheet
    Dim Count As Integer
    Set dataSheet = Worksheets("yourWorksheetName")

    Count = WorksheetFunction.CountA(dataSheet.Range(column & startRow & ":" & column & "10000")) - 1
    UserForm.Controls(cbName).List = dataSheet.Range(column & startRow & ":" & column & startRow + Count).value
End Sub

The call in your UserForm or wherever looks like this: 用户窗体中的调用或任何位置如下所示:

FillComboBox Me, Me.cbMyComboBox.name, "A", 1

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

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