问题:窗体有textbox1、textbox2,当查询的值内容不完整或不包含时,程序报错
如:输入值‘老师’。数据源值‘王老师’、‘张老师’,程序运行时则报错,输入值=张老师,时输出1条内容
Private Sub CommandButton1_Click()
If TextBox1.Value <> "" Then
If TextBox2.Value <> "" Then
Dim aData, aRes, aRef, s
Dim i As Long, j As Long, k As Long
aData = Worksheets("来料检验汇总").Range("c1").CurrentRegion
ReDim aRes(1 To UBound(aData), 1 To UBound(aData, 2))
aRef = Array(TextBox1.Value)
For i = 1 To UBound(aData)
If aData(i, 4) = TextBox2.Value Then '判断条件二
For Each s In aRef '判断是否包含关键字
If InStr(aData(i, 1), s) Then
k = k + 1
For j = 1 To UBound(aData, 2)
aRes(k, j) = aData(i, j)
Next
Exit For '退出循环
End If
Next
End If
Next
Worksheets("查询表").Select
Cells.ClearContents
Range("a2").Resize(1, UBound(aData, 2)) = aData '读取标题
Range("a3").Resize(k, UBound(aData, 2)) = aRes '查询结果
MsgBox "查询完成"
Unload Me
Exit Sub
Else
End If
MsgBox "供应商不能为空"
Exit Sub
Else
End If
MsgBox "物料名称不能为空"
End Sub
如:输入值‘老师’。数据源值‘王老师’、‘张老师’,程序运行时则报错,输入值=张老师,时输出1条内容
Private Sub CommandButton1_Click()
If TextBox1.Value <> "" Then
If TextBox2.Value <> "" Then
Dim aData, aRes, aRef, s
Dim i As Long, j As Long, k As Long
aData = Worksheets("来料检验汇总").Range("c1").CurrentRegion
ReDim aRes(1 To UBound(aData), 1 To UBound(aData, 2))
aRef = Array(TextBox1.Value)
For i = 1 To UBound(aData)
If aData(i, 4) = TextBox2.Value Then '判断条件二
For Each s In aRef '判断是否包含关键字
If InStr(aData(i, 1), s) Then
k = k + 1
For j = 1 To UBound(aData, 2)
aRes(k, j) = aData(i, j)
Next
Exit For '退出循环
End If
Next
End If
Next
Worksheets("查询表").Select
Cells.ClearContents
Range("a2").Resize(1, UBound(aData, 2)) = aData '读取标题
Range("a3").Resize(k, UBound(aData, 2)) = aRes '查询结果
MsgBox "查询完成"
Unload Me
Exit Sub
Else
End If
MsgBox "供应商不能为空"
Exit Sub
Else
End If
MsgBox "物料名称不能为空"
End Sub