我的代码无效:错误424和错误9

时间:2017-03-12 14:32:41

标签: excel-vba vba excel

我有一个excel文件,有3个工作表,统计,地理和经济学,那里有学生姓名/ ID。我写了一个带有文本框和3个选项按钮(统计,地理和经济)的用户表单和两个名为search和cancel的命令按钮。当您在文本框中写入名称并选择其中一个选项按钮时,它将在所选工作表上搜索该名称(在用户表单中将其作为选项按钮提供)。如果找到名称,那么我添加了一个标签,通知其单元格地址,如果没有找到,它会说该名称未找到。当我点击取消时,它会给我一个消息框,告诉我搜索时找不到的所有名字(我用过一个数组)。这是我写的代码:

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和名称Tom在a2,我正在寻找名称Tom,所以搜索tom将首先给我单元格tomphson。所以我用while循环来处理它。但它给了我所需的错误424对象。 第二个问题是数组。我试图打印所有未找到的名称(作为msgbox)但是当它进入我在commandbutton2命令中编写的for循环时,它给出了错误9下标超出范围。 我已经坐了一会儿,但我无法找到问题所在。我真的很感激一些帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

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

1-您不需要循环查找名称的完整匹配项,您可以使用xlPart参数代替s

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

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

您的代码修改应该有效:

{{1}}