Excel 我的代码不工作:错误424和错误9

Excel 我的代码不工作:错误424和错误9,excel,vba,Excel,Vba,我有一个excel文件,其中有三个工作表,分别是:统计、地理和经济,还有学生姓名/ID。我编写了一个用户表单,其中包含一个文本框和3个选项按钮统计、地理和经济以及两个名为搜索和取消的命令按钮。当您在文本框中写入名称并选择其中一个选项按钮时,它将在所选工作表中搜索名称,该工作表在用户表单中作为选项按钮给出。如果找到了名称,我会添加一个标签,通知它的单元格地址,如果找不到,它会说找不到名称。当我点击“取消”时,它会给我一个消息框,告诉我在搜索时没有找到的所有名字,我使用了一个数组来完成搜索。这是我写

我有一个excel文件,其中有三个工作表,分别是:统计、地理和经济,还有学生姓名/ID。我编写了一个用户表单,其中包含一个文本框和3个选项按钮统计、地理和经济以及两个名为搜索和取消的命令按钮。当您在文本框中写入名称并选择其中一个选项按钮时,它将在所选工作表中搜索名称,该工作表在用户表单中作为选项按钮给出。如果找到了名称,我会添加一个标签,通知它的单元格地址,如果找不到,它会说找不到名称。当我点击“取消”时,它会给我一个消息框,告诉我在搜索时没有找到的所有名字,我使用了一个数组来完成搜索。这是我写的代码:

Dim s(1 To 20) As String, count As Integer


Private Sub CommandButton1_Click()
    Dim wsheet As String
    If OptStat = True Then wsheet = OptStat.Caption 'OptStat.Caption = Statistics - it's the name of the worksheet called Statistics
    If OptGeo = True Then wsheet = OptGeo.Caption 'OptGeo.Caption = Geography - it's the name of the worksheet called Geography
    If OptEco = True Then wsheet = OptEco.Caption 'OptEco.caption = Economics - it's the name of the worksheet called Economics
    Worksheets(wsheet).Select

    Set r = Cells.Find(TextBox1.Text, Range("a1"), xlFormulas, xlPart, xlByRows, xlNext, False, , False)
    If r Is Nothing Then
        count = count + 1
        s(count) = TextBox1.Text & "in " & wsheet
        Label2.Caption = TextBox1.Text & " is not found in " & wsheet
        TextBox1.Text = ""
        Worksheets(1).Select
        Exit Sub
    Else
        Address = r.Address
        If TextBox1.Text = r.Value Then
            r.Activate
            Label2.Caption = TextBox1.Text & " found in worksheet " & wsheet & " on cell " & Address
            TextBox1.Text = ""
            Exit Sub
        Else
            r = Cells.FindNext(r)
            Do While r.Address <> a
                If TextBox1.Text = r.Value Then
                    r.Activate
                    Label2.Caption = TextBox1.Text & " found in worksheet " & wsheet & " on cell " & Address
                    TextBox1.Text = ""
                    Exit Sub
                Else
                    r = Cells.FindNext(r)
                End If
            Loop
            If r.Address = a Then
                count = count + 1
                s(count) = TextBox1.Text & "in " & wsheet
                Label2.Caption = TextBox1.Text & " not founf in " & wsheet
                TextBox1.Text = ""
                Exit Sub
            End If
        End If
    End If
End Sub

Private Sub CommandButton2_Click()
    Unload Me
    Dim names As String
    names = "The Following names are missing" & vbNewLine
    For i = 1 To count
        names = names & s(i) & vbNewLine
    Next i
    count = 0
    Worksheets(1).Select
    MsgBox (names)
End Sub
我的代码有两个问题,也许有人能告诉我问题是什么。第一个问题,我可能有,例如a1单元的名字Tomphson和a2单元的名字Tom,我正在寻找Tom的名字,所以搜索Tom会首先给我Tomphson单元。所以我用while循环来处理这个问题。然而,它给了我所需的错误424对象。 第二个问题是数组。我试图打印所有未作为msgbox找到的名称,但当它进入我在commandbutton2命令中编写的for循环时,它会给我错误9下标超出范围。
我已经在这上面坐了一段时间,但我找不到问题所在。我真的很感谢你的帮助。谢谢

一些修改可以简化您的代码并使其按预期工作

1-您不需要执行循环来查找名称的完整匹配项,您可以使用XLINTERAL参数而不是xlPart

2-在向数组添加新名称之前,请检查是否已达到上限

3-对于按钮2,在计算消息之前不要卸载表单,因为数组s是表单的成员,因此如果卸载它,则该数组在内存中不再有效

对代码的这种修改应该可以工作:

Private Sub CommandButton1_Click()
    Dim wsheet As String
    If OptStat = True Then wsheet = OptStat.Caption 'OptStat.Caption = Statistics - it's the name of the worksheet called Statistics
    If OptGeo = True Then wsheet = OptGeo.Caption 'OptGeo.Caption = Geography - it's the name of the worksheet called Geography
    If OptEco = True Then wsheet = OptEco.Caption 'OptEco.caption = Economics - it's the name of the worksheet called Economics

    Set r = Worksheets(wsheet).Cells.Find(TextBox1.text, Range("a1"), xlFormulas, xlWhole, xlByRows, xlNext, False, , False)
    '                                                                             ^^^^^^^^
    If Not r Is Nothing Then
        Application.Goto r
        Label2.Caption = TextBox1.text & " found in worksheet " & wsheet & " on cell " & r.address
        TextBox1.text = ""
    Else
        If Count < UBound(s) Then ' <-- Check before adding a name to array
            Count = Count + 1
            s(Count) = TextBox1.text & " in " & wsheet
        End If
        Label2.Caption = TextBox1.text & " not found in " & wsheet
        TextBox1.text = ""
    End If
End Sub

Private Sub CommandButton2_Click()
    Dim names As String
    names = "The Following names are missing" & vbNewLine
    For i = 1 To Count
        names = names & s(i) & vbNewLine
    Next i
    Count = 0
    Unload Me ' <--- here, not before, we still needed the array s
    Worksheets(1).Select
    MsgBox (names)
End Sub

谢谢整个事情实际上使我的代码比原来短得多!您能解释一下为什么第一次编写unload me命令会导致问题9下标超出范围错误吗?我在页面的开头为整个userform定义了数组,因此,如果我先关闭userform,然后执行命令,这应该无关紧要?顺便说一句,我想写一个代码来扩展我的数组,以防我达到了Redim preserve s1计数+20的上限,但它不让我这么做,你知道为什么这样不行吗?谢谢@mathstackuser123不客气,很高兴为您提供帮助:。关于Unload Me,运行时在卸载usefrform时会回收其内存,使其成员变量不再有效。至于第二个问题,这让我很惊讶,我需要看看你在哪里以及如何做这个重拨。我建议你先确认一下,然后发布一个问题,这在我看来是值得的。顺便注意,你可以使用我来取消隐藏表单。隐藏而不是卸载我,在这种情况下表单的数据仍然有效。