测试数据
输入条件
筛选结果
按钮1的源码:
Sub test()
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
Dim mybook As String
mybook = ThisWorkbook.FullName
With cnn
If Application.Version = "11.0" Then
.Provider = "microsoft.jet.oledb.4.0"
.ConnectionString = "extended properties=""excel 8.0;HDR=YES;IMEX=1"";data source=" & mybook
Else
.Provider = "microsoft.ACE.oledb.12.0"
.ConnectionString = "extended properties=""excel 12.0;HDR=YES;IMEX=1"";data source=" & mybook
End If
.Open
End With
With Worksheets("计算")
rq1 = .Range("b1")
rq2 = .Range("b2")
End With
sql = "select * from [database$a1:j] where [recorded day_记录日期] between #" & Format(rq1, "yyyy-mm-dd") & "# and #" & Format(rq2, "yyyy-mm-dd") & "#"
rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
With Worksheets("sheet1")
.Cells.Delete
For j = 0 To rs.Fields.Count - 1
.Cells(1, j + 1) = rs.Fields(j).Name
Next
.Range("a2").CopyFromRecordset rs
End With
End Sub