请修复VBA错误

时间:2018-06-14 17:15:31

标签: vba excel-vba excel

我有一个带注释的代码,指示代码不执行正确功能的位置。我希望它能显示所有可用的员工;我有3个文本框,但当3名员工在场时,它只显示在3个框中的2个。如果员工人数超过2人,我需要在msg框中显示所有员工。目前,仅显示最多2个。你能帮我解决下面的代码:

Private Sub TextBox9_data()
    Dim ws As Worksheet
    Dim i, l, m, n As Long
    Dim dict As Object
    Dim dict1 As Object
    Dim o
    Set dict = CreateObject("Scripting.Dictionary")
    Set dict1 = CreateObject("Scripting.Dictionary")

    Set ws = ThisWorkbook.Worksheets("Employee ID")
    l = 2
    With UserForm1
        m = ws.Range("B1").CurrentRegion.Rows.count
        If Trim(.TextBox4.Value) = "" Or Trim(.TextBox5.Value) = "" Or Trim(.ComboBox3.Value) = "" Then
            .TextBox9.Value = ""
            .TextBox10.Value = ""
            Exit Sub
        Else

            For i = l To m
                If UCase(Trim(ws.Range("B" & i).Value)) = UCase(Trim(.TextBox4.Value)) And _
                   UCase(Trim(ws.Range("D" & i).Value)) = UCase(Trim(.TextBox5.Value)) And _
                   UCase(Trim(ws.Range("E" & i).Value)) = UCase(Trim(.ComboBox3.Value)) Then

                    If dict.Exists(Trim(ws.Range("H" & i))) Then
                    Else
                        dict(Trim(ws.Range("H" & i))) = "" ' Does not display all imployees. Curently only displays max of 2
                        dict1(Trim(ws.Range("H" & i))) = Trim(ws.Range("H" & i)) & "-" & Trim(ws.Range("I" & i)) & " " & Trim(ws.Range("J" & i)) ' Does not display all imployees. Curently only displays max of 2
                    End If
                    If dict.count > 1 Then
                        MsgBox ("Two or more employees have worked on this. Investigate who is at fault.") & _
                                vbCrLf & "Total employees found: " & dict.count _
                              & vbLf & Join(dict1.Items, vbLf) ' Needs to display the total of qty of employees and list all of them. Currently only displays a max of 2
                        Exit For
                    End If
                End If
            Next

            If dict.count = 0 Then
                .TextBox9.Value = ""
                .TextBox10.Value = ""
                .TextBox17.Value = ""
            ElseIf dict.count = 1 Then
                .TextBox9.Value = dict.Keys()(0)
                .TextBox10.Value = ""
                .TextBox17.Value = ""
            ElseIf dict.count = 2 Then           ' Added additional "if" statement - This statement doesnt work
                .TextBox9.Value = dict.Keys()(0)
                .TextBox10.Value = dict.Keys()(1)
                .TextBox17.Value = ""            ' Added additional box for employee #3 - This box doesn't display
            Else
                .TextBox9.Value = dict.Keys()(0)
                .TextBox10.Value = dict.Keys()(1)
                .TextBox17.Value = dict.Keys()(2)
            End If
        End If
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

如果你试试这会怎么样?

      If dict.Count > 2 Then
            .TextBox9.Value = dict.Keys()(0)
            .TextBox10.Value = dict.Keys()(1)
            .TextBox17.Value = dict.Keys()(2)
        ElseIf dict.Count = 2 Then
           .TextBox9.Value = dict.Keys()(0)
            .TextBox10.Value = dict.Keys()(1)
            .TextBox17.Value = ""
        ElseIf dict.Count = 1 Then
             .TextBox9.Value = dict.Keys()(0)
            .TextBox10.Value = ""
            .TextBox17.Value = ""
        Else
            .TextBox9.Value = ""
            .TextBox10.Value = ""
            .TextBox17.Value = ""
        End If