遍历用户窗体的单列

Loop through single column of Userform

我在 VBA 中使用过一些用户表单,并且知道一些循环所有控件的技巧。但是,我 运行 遇到了这个问题,需要一种方法来根据 "Area" [=20 的值将行和原因列的值读入数组=] 和 "Shift"。这两列的可能值在图片中。

基本上我需要的是

For Each ctl In Me.Controls
    If somectl.Value = "Kitting" And otherctl.Value = "1" Then
        ReDim Preserve somearray(i)
        somearray(i) = ctl.Value
    End If
Next ctl

如果你已经设法标准化你的命名,你可以这样做:

Private Sub CommandButton1_Click()
    Dim i As Integer
    Dim areaCB As MSForms.ComboBox
    Dim shiftCB As MSForms.ComboBox
    Dim reasonCB  As MSForms.ComboBox
    Dim somearray

    For i = 1 To 3 ' 3 or more depending on how may you have in your form
        Set areaCB = Me.Controls("areadd" & i)
        Set shiftCB = Me.Controls("shiftdd" & i)
        Set reasonCB = Me.Controls("reasondd" & i)

        If areaCB.Value = "Kitting" _
        And shiftCB.Value = "1" Then
            If IsArray(somearray) Then
                ReDim Preserve somearray(UBound(somearray) + 1)
                somearray(UBound(somearray)) = reasonCB.Value
            Else
                somearray = Array(reasonCB.Value)
            End If
        End If
    Next
End Sub

所以例如在areadd1中,1是行号。
对应的,它右边旁边的ComboBox就是shiftdd1等等
这只是给你一个想法。修改它以满足您的需要。