“下标超出范围”

时间:2019-07-28 14:02:15

标签: excel vba

我要在Excel工作簿中搜索数据

此代码:

Private Sub search_Click()
    Dim totRows As Long, i As Long

    totRows = Worksheets("Sheet4").Range("A1").CurrentRegion.Rows.Count

    If kodebus.Text = "" Then
        MsgBox "Enter the code you wish to search!"
    End If

    For i = 2 To totRows
        If Trim(Sheet4.Cells(i, 1)) <> Trim(kodebus.Text) And i = totRows Then
            MsgBox "Code not found!"
        End If
        If Trim(Sheet4.Cells(i, 1)) = Trim(kodebus.Text) Then
            kodebus.Text = Sheet4.Cells(i, 1)
            namabus.Text = Sheet4.Cells(i, 2)
            jurusanbus.Text = Sheet4.Cells(i, 3)
            hargabus.Text = Sheet4.Cells(i, 4)
            Exit For
        End If
    Next i
End Sub

最后我得到一个错误,该消息是“下标超出范围”。
并在代码中获得错误位置:

totRows = Worksheets("Sheet4").Range("A1").CurrentRegion.Rows.Count

请帮助我解决它,谢谢

0 个答案:

没有答案