|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Private Sub CommandButton1_Click()
Dim ar As Variant
Dim i As Long, r As Long
Dim br()
ks = TextBox1.Text
js = TextBox2.Text
If ks = "" And js = "" Then MsgBox "您至少要输入一个查询条件!": Exit Sub
With Sheets("sheet1")
r = .Cells(Rows.Count, 1).End(xlUp).Row
If r < 5 Then MsgBox "数据源为空!": End
ar = .Range("a4:g" & r)
End With
ReDim br(1 To UBound(ar), 1 To 4)
For i = 2 To UBound(ar)
s = ar(i, 2)
If s <> "" Then
s_2 = Right(s, 1)
s_1 = Left(s, 1)
If ks <> "" And js <> "" Then
If s_1 = ks And s_2 = js Then
n = n + 1
For j = 1 To 3
br(n, j) = ar(i, j)
Next j
br(n, 4) = ar(i, 6)
End If
ElseIf ks <> "" And js = "" Then
If s_1 = ks Then
n = n + 1
For j = 1 To 3
br(n, j) = ar(i, j)
Next j
br(n, 4) = ar(i, 6)
End If
ElseIf ks = "" And js <> "" Then
If s_2 = js Then
n = n + 1
For j = 1 To 3
br(n, j) = ar(i, j)
Next j
br(n, 4) = ar(i, 6)
End If
End If
End If
Next i
If n = "" Then MsgBox "没有符合条件的数据!": Exit Sub
With Sheets("sheet2")
.[a1].CurrentRegion.Offset(1).Borders.LineStyle = 0
.[a1].CurrentRegion.Offset(1) = Empty
.[a2].Resize(n, UBound(br, 2)) = br
.[a2].Resize(n, UBound(br, 2)).Borders.LineStyle = 1
.Activate
End With
MsgBox "查询到" & n & "行数据!", 64, "提醒!"
Unload Me
End Sub
|
|