|
论坛中对于多工作簿查询的求助很多,我以此实例来给大家分享一种解决办法。代码不见的有多好,还请广大坛友斧正!!!
这个实例是同一文件夹下的多工作簿查询,如果大家要实现本文件夹及子文件夹的查询,道理基本相同,请大家结合http://club.excelhome.net/thread-1165866-1-1.html这个帖子搜索文件夹及子文件夹就可以了。
多工作簿查询-数组法.zip
(1.15 MB, 下载次数: 155)
- Public Sub 查询()
- Application.ScreenUpdating = False
- sPath = ThisWorkbook.Path & "" '获取本工作簿所在文件夹
- Dim vFill()
- sFile = Dir(sPath & "*.xls*")
- pm = [a2].Value
- pp = [b2].Value
- xh = [c2].Value
- gg = [d2].Value
- n = 0
- Do While sFile <> ""
- If sFile <> ThisWorkbook.Name Then
- With Workbooks.Open(sPath & sFile) '打开工作簿sFile
- For i = 1 To 3
-
- lr = .Sheets(i).[a65536].End(xlUp).Row
- adate = .Sheets(i).Range("a2:cj" & lr) '将第一个表的所有已用单元格的数值赋值给数组
- For j = 1 To UBound(adate)
- tj = True
-
- If pm <> "" Then tj = (adate(j, 11) Like "*" & pm & "*") And tj
- If pp <> "" Then tj = (adate(j, 12) Like "*" & pp & "*") And tj
- If xh <> "" Then tj = (adate(j, 13) Like "*" & xh & "*") And tj
- If gg <> "" Then tj = (adate(j, 14) Like "*" & gg & "*") And tj
-
- If tj Then
- n = n + 1
- ReDim Preserve vFill(1 To 89, 1 To n)
- For nCol = 1 To 88
- vFill(nCol, n) = adate(j, nCol) '复制符合条件的一行数据到查询数据数组最后一行上
- If nCol = 88 Then vFill(89, n) = sFile & "" & .Sheets(i).Name
- Next
- End If
- Next
- Next
- .Close False '关闭工作簿sFile
- End With
- End If
- sFile = Dir
- Loop
- ThisWorkbook.Activate '本工作簿激活为使用状态
- With Sheets("汇总") '对”汇总“表进行操作
- .[a4:ck65536].ClearContents
- If n > 0 Then
- .[a4].Resize(n, 89) = Application.WorksheetFunction.Transpose(vFill)
- End If
- End With
- Application.ScreenUpdating = True
- End Sub
复制代码
|
|