2017-06-13 62 views
0

我想在用户在以前的用户窗体上选择工作站后用部件号列表填充我的用户窗体组合框。用工作表中的For循环填充组合框

我的想法是遍历列,确定当字符串工作站相匹配,然后增加该小区在列的权利(这将会是产品编号)

我的代码看起来是这样的到目前为止:

If station = "MILL" Then 
    With ComboBox1 
    .AddItem "350SC109e.1" 
    .AddItem "350 SC166" 
    .AddItem "350 SC193" 
    .AddItem "350 SC195" 
    End With 
End If 

If station = "BRAKE" Then 
    For i = 2 To ws1.Range("A265").End(xlUp).Row 
    If ws1.Cells(i, 1) = "Brake" Then 
     ComboBox1.AddItem ws1.Cells(i, 2) 
    End If 
    Next i 
End If 

该工厂做手工我想用if语句制动回路来完成的事情的一个例子。

+0

ComboBox1.AddItem ws1.Cells(i,2).Value就像它看起来像你试图将一个单元格添加到组合框。 –

+0

那么,我需要写什么,所以它将单元格的值添加到组合框,而不是单元格本身? –

+0

这就是我的猜测。你没有提供关于你的问题的信息。你收到什么错误? –

回答

0

在这里我掀起了这个真正快速证明我可以得到这个工作在一个循环。你必须调整以适应你的需求。它没有完美的工作

Private Sub CommandButton1_Click() 

    Dim txtVal As String 

    If IsNull(TextBox1.Value) = False Then 
     txtVal = TextBox1.Value 
    Else 
     txtVal = "" 
    End If 

    Dim rng As Range 
    Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:A21") 
    Dim rcell As Range 

    For Each rcell In rng.Cells 
     If rcell.Value = txtVal Then 
      With ComboBox1 
       .AddItem rcell.Offset(0, 1).Value 
      End With 
     End If 
    Next rcell 

End Sub 
+0

谢谢。这真的有帮助。得到它的工作 –