|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
myyj821001 发表于 2012-6-27 16:35
太强了,我的是在数据表里,也就想用这么个窗体筛选数据,初学,对数据库还不懂
我想使用VBA窗体来实现数据的 ...
有点复杂,请测试- Dim arr
- Private Sub ComboBox1_Change()
- If ComboBox1.ListIndex = -1 Then Exit Sub
- Dim i&, m&, brr(), d As Object
- Set d = CreateObject("scripting.dictionary")
- ReDim brr(1 To UBound(arr, 2))
- For i = 1 To UBound(arr, 2)
- If arr(1, i) <> ComboBox1.Value Then
- m = m + 1
- brr(m) = arr(1, i)
- End If
- Next
- ComboBox2.List = brr
- m = ComboBox1.ListIndex + 1
- For i = 2 To UBound(arr)
- d(arr(i, m)) = ""
- Next
- ComboBox3.Clear
- ComboBox3.List = WorksheetFunction.Transpose(d.keys)
- End Sub
- Private Sub ComboBox2_Change()
- If ComboBox2.ListIndex = -1 Then Exit Sub
- Dim i&, m&, d As Object
- Set d = CreateObject("scripting.dictionary")
- m = Sheets("Sheet1").Rows(4).Find(ComboBox2.Value, , , 1).Column
- For i = 2 To UBound(arr)
- d(arr(i, m)) = ""
- Next
- ComboBox4.Clear
- ComboBox4.List = WorksheetFunction.Transpose(d.keys)
- End Sub
- Private Sub CommandButton1_Click()
- Dim cnn As Object, SQL$, s$
- Set cnn = CreateObject("ADODB.Connection")
- If ComboBox3 <> "" Then s = " and [" & ComboBox1 & "] like '" & ComboBox3 & "'"
- If ComboBox4 <> "" Then s = s & " and [" & ComboBox2 & "] like '" & ComboBox4 & "'"
- If s = "" Then Exit Sub
- cnn.Open "Provider = Microsoft.Jet.Oledb.4.0;Extended Properties =Excel 8.0;Data Source =" & ThisWorkbook.FullName
- SQL = "Select * from [Sheet1$A4:U] where " & Mid(s, 5)
- With Sheets("Sheet2")
- .UsedRange.Offset(1).ClearContents
- .[a2].CopyFromRecordset cnn.Execute(SQL)
- .Activate
- End With
- cnn.Close
- Set cnn = Nothing
- End Sub
- Private Sub CommandButton2_Click()
- Unload Me
- End Sub
- Private Sub UserForm_Initialize()
- ComboBox1.List = WorksheetFunction.Transpose(WorksheetFunction.Transpose(Sheets("Sheet1").Range("A4:U4").Value))
- arr = Sheets("Sheet1").Range("A4:U" & Sheets("Sheet1").Range("A65536").End(xlUp).Row)
- End Sub
复制代码 |
|