Excel 使用COUNTIF和SUMIF代替循环

Excel 使用COUNTIF和SUMIF代替循环,excel,vba,Excel,Vba,我有下面的工作代码,但当我按下ok时,在我的用户窗体关闭之前会有3秒钟的暂停。问题是在我的表格中只有一个结果是匹配的,所以我不认为我需要这样做,还有更有效的方法。我需要做的就是找到与名称匹配的行,然后用值填充该行的5列。这是适合COUNTIF和SUMIF的吗?我试着试一试,但我无法让它停止向我抛出错误,这让我质疑我是否做了正确的事情 这是更新后的代码。一切都在按预期进行,但仍然得到了大坝暂停,这真的不是什么大问题,我只是想理解为什么,因为我喜欢学习。我确实认为用另一种方法来寻找单一匹配会更好 P

我有下面的工作代码,但当我按下ok时,在我的用户窗体关闭之前会有3秒钟的暂停。问题是在我的表格中只有一个结果是匹配的,所以我不认为我需要这样做,还有更有效的方法。我需要做的就是找到与名称匹配的行,然后用值填充该行的5列。这是适合COUNTIF和SUMIF的吗?我试着试一试,但我无法让它停止向我抛出错误,这让我质疑我是否做了正确的事情

这是更新后的代码。一切都在按预期进行,但仍然得到了大坝暂停,这真的不是什么大问题,我只是想理解为什么,因为我喜欢学习。我确实认为用另一种方法来寻找单一匹配会更好

Private Sub OKButton_Click()

    If Me.ComboBox1.Value = vbNullString Then
        MsgBox "Please Select a member of staff", , "Error"
        Exit Sub
    End If


    With TimetableUserForm
        .Hide
        If CheckBox1.Value = False And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox4.Value = False And CheckBox5.Value = False Then
            MsgBox "Error", , "Error"
            Exit Sub
        Else
            Dim EmployeeName As String, f As range, tbl As ListObject, Counter As Integer, LastRow As Long, listcolumns As range
            EmployeeName = ComboBox1.Value
            With Sheets("Timetable").ListObjects("TblTimetable")
                LastRow = .range.Rows.Count
                For Counter = LastRow To 1 Step -1
                Set f = .DataBodyRange.Cells(Counter, .listcolumns("Name and Surname").Index)
                If f = EmployeeName Then
                    If TextBox1.Value = vbNullString And TextBox2.Value = vbNullString And TextBox3.Value = vbNullString And TextBox4.Value = vbNullString And TextBox5.Value = vbNullString Then
                        With f.EntireRow
                            .Cells(5).Value = CheckBox1.Value
                            .Cells(6).Value = CheckBox2.Value
                            .Cells(7).Value = CheckBox3.Value
                            .Cells(8).Value = CheckBox4.Value
                            .Cells(9).Value = CheckBox5.Value
                        End With
                    Else
                        With f.EntireRow
                            .Cells(5).Value = TextBox1.Value
                            .Cells(6).Value = TextBox2.Value
                            .Cells(7).Value = TextBox3.Value
                            .Cells(8).Value = TextBox4.Value
                            .Cells(9).Value = TextBox5.Value
                        End With
                    End If
                    Exit For
                End If
                Next Counter
            End With
        End If
    End With
    Unload Me
End Sub
下面是打开userform的宏

Private Sub Rectangle_16_Click()
If Sheets("Settings").range("Protected") = 1 Then
            'do nothing
            Else
TimetableUserForm.Show
End If
End Sub

在填充单元格后立即为设置一个退出按钮

此外,这里还有一些建议:

Private Sub OKButton_Click()

    With Me ' reference the Userform
        .Hide ' hide the userform

        If .ComboBox1.Value = vbNullString Then
            MsgBox "Please Select a member of staff", , "Error"
            Exit Sub
        End If

        If .CheckBox1 Or .CheckBox2 Or .CheckBox3 Or .CheckBox4 Or .CheckBox5 Then
            Dim EmployeeName As String, f As Range, tbl As ListObject, Counter As Integer, LastRow As Long, listcolumns As Range
            EmployeeName = .ComboBox1.Value
            With Sheets("Timetable").ListObjects("TblTimetable")
                LastRow = .Range.Rows.Count
                For Counter = LastRow To 1 Step -1
                    Set f = .DataBodyRange.Cells(Counter, .listcolumns("Name and Surname").Index)
                    If f = EmployeeName Then
                        With f.EntireRow
                            .Cells(5).Value = Me.TextBox1.Value
                            .Cells(6).Value = Me.TextBox2.Value
                            .Cells(7).Value = Me.TextBox3.Value
                            .Cells(8).Value = Me.TextBox4.Value
                            .Cells(9).Value = Me.TextBox5.Value
                        End With
                        Exit For
                    End If
                Next
            End With
        End If

    End With

'    Unload Me ' <-- move Unload command in the sub that has shown the userform
End Sub

在上一篇文章中,您已经得到了关于“更有效的方法”的建议—使用
Find()
是一种选择,或者您可以使用
Match()
。要找出3秒钟的时间,你可以使用定时器进行调试。打印或MsgBox嘿,查找你发布的代码不起作用我不知道是什么,我想既然我们在那张表中使用当前方法得到了代码,我应该关闭该问题并开始新的问题,寻找更好的方法。我尝试使用debug.print和即时窗口,但我以前从未使用过它,我也不知道我在做什么,哈哈,我会看看你现在发布的链接这太棒了!我如何移动卸载我到发射用户窗体的sub?我不知道放在哪里?将“”更改为vbNullString的原因是什么?我知道你的方式更完整。我保证我在那里添加了exit,它只是导致我的非值被插入,但你已经做了,它工作得很好,原因是什么?好的,性能它会立即关闭并发送我的值,但在我按“确定”后windows沙漏会显示几秒钟,正常?使用me.textbox而不仅仅是文本框的原因是什么?1)更新显示代码的问题激发用户表单2)使用vbNullString代替“”优化性能,如果我用“强制编译器创建一个空字符串变量3”来更新它,请用“4小时出口”来显示你不工作代码的问题),至于Windows时刻表,我想你的表正在做一些计算5)如果答案解决了你的问题,你可以考虑把它标记为接受更新,谢谢。很高兴知道关于vbnullstring我将更改工作表中“”的每个实例暂停不再明显,因为我已将按钮移动到另一个工作表,它现在工作正常,我只想了解您对unload me命令的看法,因为您认为该命令位于错误的位置。
Private Sub Rectangle_16_Click()
    If Sheets("Settings").range("Protected") = 1 Then
            'do nothing
    Else
        With New TimetableUserForm ' instantiate a new instance of your Userform class and reference 
            .Show ' show it
        End With ' this will set the reference object to Nothing      
    End If
End Sub