Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在组合框中不出现重复名称(A列)并在新组合框中填充B列名称_Excel_Vba_Userform - Fatal编程技术网

Excel 如何在组合框中不出现重复名称(A列)并在新组合框中填充B列名称

Excel 如何在组合框中不出现重复名称(A列)并在新组合框中填充B列名称,excel,vba,userform,Excel,Vba,Userform,我遇到了一个问题,我无法在“组合框1”中填充a列名称,而没有重复项。在删除重复项后,我需要通过对A列值进行分组,将相应的B列值填充到“Combo Box2”中。我的代码中是否有导致此问题的内容?任何帮助都将不胜感激 我的Excel数据的下图: 输出:当从“组合框1”中选择“A”(不重复值3次)时,应在“组合框2”中填充“12,2,3” 下面是将名称链接到组合框的代码 Private Sub cboproj_DropButtonClick() Dim ssheet As Worksheet Se

我遇到了一个问题,我无法在“组合框1”中填充a列名称,而没有重复项。在删除重复项后,我需要通过对A列值进行分组,将相应的B列值填充到“Combo Box2”中。我的代码中是否有导致此问题的内容?任何帮助都将不胜感激

我的Excel数据的下图:

输出:当从“组合框1”中选择“A”(不重复值3次)时,应在“组合框2”中填充“12,2,3”

下面是将名称链接到组合框的代码

Private Sub cboproj_DropButtonClick()
Dim ssheet As Worksheet
Set ssheet = ThisWorkbook.Worksheets("Sheet1")
    ssheet.Activate
Dim i As Long
If Me.cboproj.ListCount = 0 Then
    For i = 2 To ssheet.Range("A" & ssheet.Rows.Count).End(xlUp).Row
        Me.cboproj.AddItem Sheets("LS numbers").Cells(i, "A").Value
        'remove duplicates
        ssheet.Columns(1).RemoveDuplicates Columns:=Array(1)

    Next i
End If
End Sub

Private Sub cboproj_Change()
Dim ws As Worksheet
Dim i As Long
Dim str As String
Set ws = ThisWorkbook.Worksheets("Sheet1")
ws.Activate
For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    If ws.Cells(i, "A").Value = (Me.cboproj) Or ws.Cells(i, "A").Value = Val(Me.cboproj) Then
        Me.cbonumber = ws.Cells(i, "B").Value  
        Me.cboloc = ws.Cells(i, "C").Value 
    End If
Next i
End Sub

首先,右键单击所有3个组合框,并将它们的
Style
fmStyleDropDownCombo
更改为
fmStyleDropDownList
。这将确保用户无法键入列表中不存在的内容

逻辑

  • UserForm\u Initialize
    中,对excel工作表中的数据进行排序。这样,在组合框中添加数据时,数据将被排序
  • 将Excel列A-B存储在数组中
  • 创建a列值的唯一集合。将集合中的项添加到Combobox1
  • 在组合框A单击事件中,将组合框A的值与数组第1列中的数据匹配,然后填充组合框B(首先清除组合框B)
  • 在组合框B单击事件中,将组合框B值与数组第2列中的数据匹配,然后填充组合框C(首先清除组合框C)
  • 我的假设

  • 数据存储在工作表中,工作表的代号为
    Sheet1
  • Part
    必须进入
    Combobox1
  • Nr.
    必须进入
    Combobox2
  • Loc
    必须进入
    Combobox3
  • 因此,当您实现以下内容时,请在代码中进行这些更改

    代码

    Option Explicit
    
    Dim MyAr As Variant
    Dim i As Long
    Dim col As Collection
    Dim itm As Variant
    
    Private Sub UserForm_Initialize()
        Dim ws As Worksheet
        Dim lRow As Long
    
        Set col = New Collection
    
        '~~> Set this to the relevant sheet
        Set ws = Sheet1
    
        With ws
            lRow = .Range("A" & .Rows.Count).End(xlUp).Row
    
            '~~> Sort Col A,B and C so that you get sorted data inthe combobox
            With .Sort
                .SortFields.Clear
    
                .SortFields.Add Key:=Range("A2:A" & lRow), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
                .SortFields.Add Key:=Range("B2:B" & lRow), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
                .SortFields.Add Key:=Range("C2:C" & lRow), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
                .SetRange Range("A1:C" & lRow)
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
    
            '~~> Store the range in an array
            MyAr = .Range("A2:C" & lRow).Value
    
            '~~> Loop through Col A of the array and
            '~~> Create a unique collection
            For i = LBound(MyAr) To UBound(MyAr)
                On Error Resume Next
                If Len(Trim(MyAr(i, 1))) <> 0 Then
                    col.Add MyAr(i, 1), CStr(MyAr(i, 1))
                End If
                On Error GoTo 0
            Next i
        End With
    
        '~~> Add items to Combobox 1
        If col.Count <> 0 Then
            For Each itm In col
                ComboBox1.AddItem itm
            Next
        End If
    End Sub
    
    Private Sub ComboBox1_Click()
        If ComboBox1.ListIndex = -1 Then Exit Sub
    
        '~~> Clear for input
        ComboBox2.Clear
        ComboBox3.Clear
    
        '~~> Compare array and fill combobox 2
        For i = LBound(MyAr) To UBound(MyAr)
            If MyAr(i, 1) = ComboBox1.Value Then
               ComboBox2.AddItem MyAr(i, 2)
            End If
        Next i
    End Sub
    
    Private Sub ComboBox2_Click()
        If ComboBox2.ListIndex = -1 Then Exit Sub
    
        ComboBox3.Clear
    
        Set col = New Collection
    
        '~~> Compare array and create a unique collection
        For i = LBound(MyAr) To UBound(MyAr)
            If Trim(MyAr(i, 1)) = (ComboBox1.Value) And _
               Trim(MyAr(i, 2)) = (ComboBox2.Value) Then
                On Error Resume Next
                col.Add MyAr(i, 3), CStr(MyAr(i, 3))
                On Error GoTo 0
            End If
        Next i
    
        '~~> Fill combobox 3
        If col.Count <> 0 Then
            For Each itm In col
                ComboBox3.AddItem itm
            Next
        End If
    End Sub
    
    选项显式
    变暗MyAr
    我想我会坚持多久
    收藏
    Dim itm作为变体
    私有子用户表单_初始化()
    将ws设置为工作表
    暗淡的光线和长的一样
    Set col=新集合
    “~~>将其设置为相关的工作表
    设置ws=Sheet1
    与ws
    lRow=.Range(“A”&.Rows.Count).End(xlUp).Row
    “~~>对列A、B和C进行排序,以便在组合框中获得排序后的数据
    用。排序
    .SortFields.Clear
    .SortFields.Add Key:=范围(“A2:A”和lRow)_
    SortOn:=xlSortOnValues,顺序:=XLASSENDING,数据选项:=xlSortNormal
    .SortFields.Add Key:=范围(“B2:B”和lRow)_
    SortOn:=xlSortOnValues,顺序:=XLASSENDING,数据选项:=xlSortNormal
    .SortFields.Add Key:=范围(“C2:C”和lRow)_
    SortOn:=xlSortOnValues,顺序:=XLASSENDING,数据选项:=xlSortNormal
    .SetRange范围(“A1:C”和lRow)
    .Header=xlYes
    .MatchCase=False
    .方向=xlTopToBottom
    .SortMethod=xl拼音
    .申请
    以
    “~~>将范围存储在数组中
    MyAr=.Range(“A2:C”&lRow).Value
    “~~>通过数组的A列循环,然后
    “~~>创建一个唯一的集合
    对于i=LBound(MyAr)到UBound(MyAr)
    出错时继续下一步
    如果Len(Trim(MyAr(i,1)))为0,则
    第二栏加上迈亚尔(一,1),集体安全条约组(迈亚尔(一,1))
    如果结束
    错误转到0
    接下来我
    以
    “~~>将项目添加到组合框1
    如果列数为0,则
    对于每个itm,以col为单位
    ComboBox1.AddItem itm
    下一个
    如果结束
    端接头
    专用子组合框1_单击()
    如果ComboBox1.ListIndex=-1,则退出Sub
    “~~>清除输入
    组合框2.清除
    组合框3.清除
    “~~>比较数组和填充组合框2
    对于i=LBound(MyAr)到UBound(MyAr)
    如果MyAr(i,1)=ComboBox1.值,则
    ComboBox2.AddItem MyAr(i,2)
    如果结束
    接下来我
    端接头
    专用子组合框2_单击()
    如果ComboBox2.ListIndex=-1,则退出Sub
    组合框3.清除
    Set col=新集合
    “~~>比较数组并创建唯一的集合
    对于i=LBound(MyAr)到UBound(MyAr)
    如果修剪(MyAr(i,1))=(ComboBox1.Value)和_
    Trim(MyAr(i,2))=(ComboBox2.Value)然后
    出错时继续下一步
    第二栏加上迈亚尔(一,3),集体安全条约组(迈亚尔(一,3))
    错误转到0
    如果结束
    接下来我
    “~~>填充组合框3
    如果列数为0,则
    对于每个itm,以col为单位
    ComboBox3.AddItem itm
    下一个
    如果结束
    端接头
    
    在行动中


    首先,右键单击所有3个组合框,将它们的
    样式
    fmStyleDropDownCombo
    更改为
    fmStyleDropDownList
    。这将确保用户无法键入列表中不存在的内容

    逻辑

  • UserForm\u Initialize
    中,对excel工作表中的数据进行排序。这样,在组合框中添加数据时,数据将被排序
  • 将Excel列A-B存储在数组中
  • 创建a列值的唯一集合。将集合中的项添加到Combobox1
  • 在组合框A单击事件中,将组合框A的值与数组第1列中的数据匹配,然后填充组合框B(首先清除组合框B)
  • 在组合框B单击事件中,将组合框B值与数组第2列中的数据匹配,然后填充组合框C(首先清除组合框C)
  • 我的假设

  • 数据存储在工作表中,工作表的代号为
    Sheet1
  • Part
    必须进入
    Combobox1
  • Nr.
    必须进入
    Combobox2
  • Loc
    必须进入
    Combobox3
  • 因此,当您实现以下内容时,请在代码中进行这些更改

    代码

    Option Explicit
    
    Dim MyAr As Variant
    Dim i As Long
    Dim col As Collection
    Dim itm As Variant
    
    Private Sub UserForm_Initialize()
        Dim ws As Worksheet
        Dim lRow As Long
    
        Set col = New Collection
    
        '~~> Set this to the relevant sheet
        Set ws = Sheet1
    
        With ws
            lRow = .Range("A" & .Rows.Count).End(xlUp).Row
    
            '~~> Sort Col A,B and C so that you get sorted data inthe combobox
            With .Sort
                .SortFields.Clear
    
                .SortFields.Add Key:=Range("A2:A" & lRow), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
                .SortFields.Add Key:=Range("B2:B" & lRow), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
                .SortFields.Add Key:=Range("C2:C" & lRow), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
                .SetRange Range("A1:C" & lRow)
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
    
            '~~> Store the range in an array
            MyAr = .Range("A2:C" & lRow).Value
    
            '~~> Loop through Col A of the array and
            '~~> Create a unique collection
            For i = LBound(MyAr) To UBound(MyAr)
                On Error Resume Next
                If Len(Trim(MyAr(i, 1))) <> 0 Then
                    col.Add MyAr(i, 1), CStr(MyAr(i, 1))
                End If
                On Error GoTo 0
            Next i
        End With
    
        '~~> Add items to Combobox 1
        If col.Count <> 0 Then
            For Each itm In col
                ComboBox1.AddItem itm
            Next
        End If
    End Sub
    
    Private Sub ComboBox1_Click()
        If ComboBox1.ListIndex = -1 Then Exit Sub
    
        '~~> Clear for input
        ComboBox2.Clear
        ComboBox3.Clear
    
        '~~> Compare array and fill combobox 2
        For i = LBound(MyAr) To UBound(MyAr)
            If MyAr(i, 1) = ComboBox1.Value Then
               ComboBox2.AddItem MyAr(i, 2)
            End If
        Next i
    End Sub
    
    Private Sub ComboBox2_Click()
        If ComboBox2.ListIndex = -1 Then Exit Sub
    
        ComboBox3.Clear
    
        Set col = New Collection
    
        '~~> Compare array and create a unique collection
        For i = LBound(MyAr) To UBound(MyAr)
            If Trim(MyAr(i, 1)) = (ComboBox1.Value) And _
               Trim(MyAr(i, 2)) = (ComboBox2.Value) Then
                On Error Resume Next
                col.Add MyAr(i, 3), CStr(MyAr(i, 3))
                On Error GoTo 0
            End If
        Next i
    
        '~~> Fill combobox 3
        If col.Count <> 0 Then
            For Each itm In col
                ComboBox3.AddItem itm
            Next
        End If
    End Sub
    
    选项显式
    D