Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2653|回复: 11

[已解决]单张查询改批量查询的问题

[复制链接]
发表于 2013-7-10 18:03 | 显示全部楼层 |阅读模式
附件 提取数据附件.rar (141.93 KB, 下载次数: 12)
发表于 2013-7-10 22:00 | 显示全部楼层
查询到的所有结果汇总到代码所在工作簿的SHEET0里?
回复

使用道具 举报

发表于 2013-7-10 22:21 | 显示全部楼层
插入一个模块,贴粘代码。
  1. Sub CommandButton1_Click()
  2.     Dim strPath As String, strFile As String
  3.     strPath = ThisWorkbook.Path & Application.PathSeparator & "数据文件" & Application.PathSeparator
  4.     strFile = Dir(strPath & "*.xls")
  5.     With Application
  6.         .ScreenUpdating = False
  7.         .DisplayAlerts = False
  8.         .EnableEvents = False
  9.         .Calculation = xlCalculationManual
  10.     End With

  11.     Do While Len(strFile)
  12.         If strFile <> ThisWorkbook.Name Then
  13.             Call Query(strPath & strFile)
  14.         End If
  15.         strFile = Dir
  16.     Loop
  17.     With Application
  18.         .ScreenUpdating = True
  19.         .DisplayAlerts = True
  20.         .EnableEvents = True
  21.         .Calculation = xlCalculationAutomatic
  22.     End With
  23.     MsgBox "汇总完成"
  24.     Application.StatusBar = ""
  25. End Sub

  26. Sub Query(strFullname As String)
  27.     Dim objwb As Workbook
  28.     Dim objsh As Worksheet
  29.     Dim objDst As Worksheet
  30.     Dim arr, i&, j&
  31.     Set objwb = GetObject(strFullname)
  32.     Set objDst = ThisWorkbook.Worksheets("sheet0")
  33.     Windows(objwb.Name).Visible = True
  34.     Application.StatusBar = strFullname
  35.     For Each objsh In objwb.Worksheets
  36.         With objsh
  37.             If .Name <> "Sheet0" Then
  38.                 arr = .Range(.Cells(1, 1), .Cells(.[a65536].End(3).Row, .[iv1].End(1).Column)).Value
  39.                 For i = 1 To UBound(arr)
  40.                     For j = i + 1 To UBound(arr)
  41.                         If arr(i, 4) = arr(j, 4) And arr(i, 10) = arr(j, 10) And arr(i, 11) = arr(j, 11) And arr(i, 13) = arr(j, 13) And arr(i, 14) = arr(j, 14) And arr(i, 16) = arr(j, 16) Then
  42.                             objDst.Cells([b65536].End(3).Row + 2, 1).Resize(, UBound(arr, 2)) = Application.Index(arr, i, 0)
  43.                             objDst.Cells([b65536].End(3).Row + 1, 1).Resize(, UBound(arr, 2)) = Application.Index(arr, j, 0)
  44.                             objDst.Cells([b65536].End(3).Row, 18) = Right(.Name, Len(.Name) - 5)
  45.                         End If
  46.                     Next j
  47.                 Next i
  48.             End If
  49.         End With
  50.     Next objsh
  51.     objwb.Close False
  52. End Sub
复制代码
回复

使用道具 举报

发表于 2013-7-10 22:22 | 显示全部楼层
没做容错处理。
回复

使用道具 举报

 楼主| 发表于 2013-7-10 22:40 | 显示全部楼层
hwc2ycy 发表于 2013-7-10 22:22
没做容错处理。

谢谢老师帮助。查询到的结果,应返回各自的工作薄的Sheet0里。
回复

使用道具 举报

发表于 2013-7-10 22:53 | 显示全部楼层
  1. Sub Query(strFullname As String)
  2.     Dim objwb As Workbook
  3.     Dim objsh As Worksheet
  4.     Dim objDst As Worksheet
  5.     Dim arr, i&, j&
  6.     Set objwb = GetObject(strFullname)
  7.     Set objDst = objwb.Worksheets("sheet0")
  8.     Windows(objwb.Name).Visible = True
  9.     Application.StatusBar = strFullname
  10.     For Each objsh In objwb.Worksheets
  11.         With objsh
  12.             If .Name <> "Sheet0" Then
  13.                 arr = .Range(.Cells(1, 1), .Cells(.[a65536].End(3).Row, .[iv1].End(1).Column)).Value
  14.                 For i = 1 To UBound(arr)
  15.                     For j = i + 1 To UBound(arr)
  16.                         If arr(i, 4) = arr(j, 4) And arr(i, 10) = arr(j, 10) And arr(i, 11) = arr(j, 11) And arr(i, 13) = arr(j, 13) And arr(i, 14) = arr(j, 14) And arr(i, 16) = arr(j, 16) Then
  17.                             objDst.Cells([b65536].End(3).Row + 2, 1).Resize(, UBound(arr, 2)) = Application.Index(arr, i, 0)
  18.                             objDst.Cells([b65536].End(3).Row + 1, 1).Resize(, UBound(arr, 2)) = Application.Index(arr, j, 0)
  19.                             objDst.Cells([b65536].End(3).Row, 18) = Right(.Name, Len(.Name) - 5)
  20.                         End If
  21.                     Next j
  22.                 Next i
  23.             End If
  24.         End With
  25.     Next objsh
  26.     objwb.Close False
  27. End Sub
复制代码
你试下,替换这个过程。
这个过程改了几个字而已。
回复

使用道具 举报

 楼主| 发表于 2013-7-10 23:17 | 显示全部楼层
hwc2ycy 发表于 2013-7-10 22:53
你试下,替换这个过程。
这个过程改了几个字而已。

谢谢老师。试了一下,能正常运行,但无结果。

附件 查询.rar (439.83 KB, 下载次数: 14)
回复

使用道具 举报

发表于 2013-7-11 06:55 | 显示全部楼层
忘了,你看最后一句是CLOSE FALSE,{:1012:}
  1. Sub Query(strFullname As String)
  2.     Dim objwb As Workbook
  3.     Dim objsh As Worksheet
  4.     Dim objDst As Worksheet
  5.     Dim arr, i&, j&
  6.     Set objwb = GetObject(strFullname)
  7.     Set objDst = objwb.Worksheets("sheet0")
  8.     Windows(objwb.Name).Visible = True
  9.     Application.StatusBar = strFullname
  10.     For Each objsh In objwb.Worksheets
  11.         With objsh
  12.             If .Name <> "Sheet0" Then
  13.                 arr = .Range(.Cells(1, 1), .Cells(.[a65536].End(3).Row, .[iv1].End(1).Column)).Value
  14.                 For i = 1 To UBound(arr)
  15.                     For j = i + 1 To UBound(arr)
  16.                         If arr(i, 4) = arr(j, 4) And arr(i, 10) = arr(j, 10) And arr(i, 11) = arr(j, 11) And arr(i, 13) = arr(j, 13) And arr(i, 14) = arr(j, 14) And arr(i, 16) = arr(j, 16) Then
  17.                             objDst.Cells([b65536].End(3).Row + 2, 1).Resize(, UBound(arr, 2)) = Application.Index(arr, i, 0)
  18.                             objDst.Cells([b65536].End(3).Row + 1, 1).Resize(, UBound(arr, 2)) = Application.Index(arr, j, 0)
  19.                             objDst.Cells([b65536].End(3).Row, 18) = Right(.Name, Len(.Name) - 5)
  20.                         End If
  21.                     Next j
  22.                 Next i
  23.             End If
  24.         End With
  25.     Next objsh
  26.     objwb.Close TRUE
  27. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-7-11 07:40 | 显示全部楼层
hwc2ycy 发表于 2013-7-11 06:55
忘了,你看最后一句是CLOSE FALSE,

谢谢老师帮助。试了一下,出现无限循环。查了工作薄1,接着查工作薄2,再1,再2,再1,再2,停不下来。
回复

使用道具 举报

发表于 2013-7-11 08:49 | 显示全部楼层
应该不会啊。上面的循环不会死的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 06:01 , Processed in 0.373741 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表