组合框,返回ID的所有数字

时间:2019-06-11 14:55:02

标签: excel vba

我正在尝试在Userform中创建一个组合框,该组合框返回相应“ Cliente”的所有“ Aditivo”编号...

如果我选择“ Cliente” 1,则应该在组合框中添加数字1,2、3和4 ... 如果我选择“ Cliente” 3,它应该在组合框中添加数字1,2、3、4和5 ...

enter image description here

Here you put the "Cliente" ID and it should returns all numbers in field above

Private Sub CommandButton2_Click()

    Dim ID_CL, ID_Row, i As Integer
    Dim ID_Address As String

    ID_CL = 5

    ' Find the address of the ID_CL value
    On Error GoTo Erro
    ID_Address = Sheets("TAB_Aditivos").Range("A2:A16").Find(ID_CL).Address

    ' Get the number of row where the value is found
    ID_Row = Right(ID_Address, Len(ID_Address) - 3)


    UserForm1.CB_Aditivo.Enabled = True
    UserForm1.CB_Aditivo.BackColor = RGB(255, 255, 255)

    For i = 0 To 10000
        ' Check if the value above is different than ID_CL
        If Sheets("TAB_Aditivos").Cells(i + ID_Row, 1) = ID_CL Then
            ' Find the value in column "B"
            UserForm1.CB_Aditivo.AddItem (Sheets("TAB_Aditivos").Cells(i + ID_Row, 2).Text)
        Else
            ' If it's different then set i to 10000 and stop looping
            i = 10000
        End If
    Next i

    Exit Sub

Erro:
MsgBox "Value not found!", vbCritical

End Sub

这是我所做的代码,但是有一个问题,如果TAB_Aditivos中的值不正确,那么它将无法正常工作...而且我不知道它是否有效……

0 个答案:

没有答案