|
- Sub 筛选()
- Dim arr, a(), n%, i&, j%, key$, t1 As Date, t2 As Date
- Rem 数据准备
- If Range("M1") = "" Then MsgBox "请输入井号!": Range("M1").Select: Exit Sub
- If Range("O1") = "" Or Not IsDate(Range("O1")) Then MsgBox "请输入正确的开始日期!": Range("O1").Select: Exit Sub
- If Range("Q1") = "" Or Not IsDate(Range("Q1")) Then MsgBox "请输入正确的结束日期!": Range("Q1").Select: Exit Sub
- key = Range("M1"): t1 = Range("O1"): t2 = Range("Q1")
- If t1 > t2 Then MsgBox "结束日期不得小于开始日期!": Exit Sub
- arr = Sheet1.Range("A1").CurrentRegion
- Rem 筛选
- For i = 2 To UBound(arr)
- If arr(i, 1) = key And arr(i, 2) >= t1 And arr(i, 2) <= t2 Then
- n = n + 1: ReDim Preserve a(1 To 7, 1 To n)
- For j = 1 To 7
- a(j, n) = arr(i, j)
- Next
- End If
- Next
- Rem 输出筛选结果
- With Sheet1.Range("L4:R65536")
- .Borders.LineStyle = xlNone
- .ClearContents
- End With
- If n > 0 Then
- With Sheet1.Range("L4").Resize(n, 7)
- .Value = WorksheetFunction.Transpose(a)
- .Borders.LineStyle = xlContinuous
- End With
- Else
- MsgBox "没有匹配到数据!"
- End If
- End Sub
复制代码 |
|