如何使用vba在excel工作表中搜索数据

时间:2018-02-24 07:42:48

标签: excel-vba vba excel

使用userform按钮从工作表中搜索数据时遇到问题。

runtime error 424 object required已生成。

我的工作表数据从第7行开始

这是我的代码。

Private Sub cmdSearch_Click()
Dim totRows As Long
Dim i As Long
'Dim currentregion As Long


totRows = Worksheets("MainSheet").Range("C7").currentregion.Rows.count

If ComboBox5.Value = "" Then
MsgBox "enter case number to search!!"
End If


For i = 7 To totRows
     If Trim(MainSheet.Cells(i, 3)) = Trim(ComboBox5.Value) Then
        ComboBox9.Value = MainSheet.Cells(i, 2)
        ComboBox1.Value = MainSheet.Cells(i, 4)
        ComboBox2.Value = MainSheet.Cells(i, 1)
        ComboBox5.Value = MainSheet.Cells(i, 3)
        ComboBox8.Value = MainSheet.Cells(i, 5)
        ComboBox9.Value = MainSheet.Cells(i, 6)
        ComboBox10.Value = MainSheet.Cells(i, 7)
        ComboBox11.Value = MainSheet.Cells(i, 8)
        ComboBox12.Value = MainSheet.Cells(i, 9)
        ComboBox15.Value = MainSheet.Cells(i, 10)
        ComboBox16.Value = MainSheet.Cells(i, 11)
        ComboBox17.Value = MainSheet.Cells(i, 16)
        ComboBox18.Value = MainSheet.Cells(i, 22)
        ComboBox22.Value = MainSheet.Cells(i, 17)
        ComboBox21.Value = MainSheet.Cells(i, 23)
        TextBox1.Value = MainSheet.Cells(i, 13)
        TextBox2.Value = MainSheet.Cells(i, 14)
        TextBox3.Value = MainSheet.Cells(i, 15)
        TextBox4.Value = MainSheet.Cells(i, 19)
        TextBox5.Value = MainSheet.Cells(i, 20)
        TextBox6.Value = MainSheet.Cells(i, 21)
        TextBox7.Value = MainSheet.Cells(i, 18)
        TextBox8.Value = MainSheet.Cells(i, 12)

        Exit For
     End If
Next i

End Sub

0 个答案:

没有答案