我想要一种通用方法来填充 Microsoft Visual Basic for Application 7.1 中的组合框。
我尝试使用 CType
将控制参数转换为组合框,但 Excel 无法识别此函数。
如何以通用方式填充组合框?
最佳答案
最简单的方法是通过组合框的名称。您不必将 control
强制转换为 comboBox
。
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
在您的用户表单中或任何看起来像这样的地方进行调用:
FillComboBox Me, Me.cbMyComboBox.name, "A", 1
关于vba - 如何在 Excel VBA 中创建通用组合框填充例程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46505680/