填充动态组合框

时间:2014-10-09 08:30:09

标签: vba excel-vba excel

我在运行时添加了许多控件,但我希望根据从其他动态控件(难以识别的单词)收到的答案填充在运行时创建的组合框!

我会尝试通过我的代码解释!在运行期间,以下控件将添加到我的用户窗体中:

Set cLabel = Me.Controls.Add("Forms.Label.1")
With cLabel
    .Caption = "Do you study maths?"
    .Font.Size = 8
    .Font.Bold = False
    .Font.Name = "Tahoma"
    .Left = 38
    .Top = mathslbl
    .Width = 220
End With

Set cManagedOptionButtonYes= Me.Controls.Add("Forms.OptionButton.1")
With cManagedOptionButtonYes
    .Caption = "Yes"
    .Name = "fibreRingYes" & i
    .GroupName = "fibreRing" & i
    .Left = 160
    .Top = mathsYes
    .Width = 100
    .Height = 15.75
End With

ReDim Preserve TextListBox(1 To i)
Set TextListBox(i).mathsGroupYes = cManagedOptionButtonYes

Set cStudentDropdown = Me.Controls.Add("Forms.Combobox.1")
With cStudentDropdown 
    .Name = "StudenDropdown1" & (i)
    .Left = 50
    .Top = studentCombobox
    .Width = 130
    .Height = 18
 End With

 ReDim Preserve TextListBox(1 To i)
 Set TextListBox(i).studentdropdown1Group = cStudentDropdown 

因此,在运行期间会创建以下控件,这些控件会询问学生是否学习数学,并且取决于用户给出的组合框的答案。以下是根据用户对问题的回答尝试填充组合框的代码:

Private Sub cManagedOptionButtonYes_Click()
    Dim rng As Range
    Dim cell As Range
    Dim c As Range

    With cStudentDropdown 
        Sheets("Students").Activate
        For Each c In Sheets("Students").Range("C14:C86")
             .AddItem c.Value
        Next
    End With
End Sub

当代码尝试执行时,我收到以下错误Object variable or with block variable not set,我无法弄清楚为什么这对我不起作用!

1 个答案:

答案 0 :(得分:0)

假设这是在userform的代码页中:

Private Sub cManagedOptionButtonYes_Click()
'Dim rng As Range
'Dim cell As Range
Dim c As Range

With Me.cStudentDropdown   'Me. is not necessary, but i like to use it in order to have a dropdown menu with all functions and controls created on the form
    'Sheets("Students").Activate
    .clear
    For Each c In Sheets("Students").Range("C14:C86")
         '.AddItem c.Value 'should work (whatever its format i think)..., so try this :
         'make sure no cell in the looping range returns a error result such as #N/A something like that
         .additem
         .list(.listcount-1) = c.value 'or try c.value2
    Next
End With
End Sub

`

相关问题