|
- Set SHX = Worksheets("Sheet3")
- SHX.Range("A5:Z65536").ClearContents
-
- Rem 全部文件
- FileArr = FileAllArr(ThisWorkbook.Path, "*.xls?", ThisWorkbook.Name, True, False)
- For I = 0 To UBound(FileArr)
-
- Set WB = Workbooks.Open(FileArr(I))
- Str_coon = "HDR=yes';Data Source =" & FileArr(I) '//OFFICE2003,2007 通用
- For Each SH In WB.Worksheets
- Rem 提示信息,在状态栏显示
- Application.StatusBar = "查询文件总数:" & UBound(FileArr) + 1 & " 当前是第:" & I + 1 & " 当前查询的文件是:" & GetPathFromFileName(FileArr(I), True) & " 正在查询的工作表是:" & SH.Name
- DoEvents
- Rem 判断是否是所需工作表
- If SHX.Range("B2").Value = SH.Range("A" & SHX.Range("B1").Value).Value Then
- Rem 组合查询标题
- StrSQL = "SELECT "
- For ICOL = 1 To SHX.Range("AZ4").End(xlToLeft).Column
- If ICOL > 1 Then StrSQL = StrSQL & ","
- If SHX.Cells(4, ICOL).Value = "来自文件" Then
- StrSQL = StrSQL & "'" & GetPathFromFileName(FileArr(I)) & "' AS 来自文件"
- Else
- StrSQL = StrSQL & SHX.Cells(4, ICOL).Value
- End If
- Next
- Rem 查询条件
- StrSQL = StrSQL & " FROM [" & SH.Name & "$A" & SHX.Range("B1").Value & ":AZ]"
- If Len(SHX.Range("D1").Value) > 0 And Len(SHX.Range("D2").Value) > 0 Then
- StrSQL = StrSQL & " WHERE INSTR(" & SHX.Range("D1").Value & ",'" & SHX.Range("D2").Value & "')>0"
- End If
-
- Rem 粘贴数据
- IROW = SHX.Range("A65536").End(3).Row + 1
- SQLARR = GET_SQL_To_Arr(StrSQL, Str_coon, False)
- SHX.Range("A" & IROW).Resize(UBound(SQLARR, 1) + 1, UBound(SQLARR, 2) + 1) = SQLARR '//(0 TO X)的数组行列都要+1,(1 TO X) 的不要
- End If
- Next
- WB.Close True '//保存
- Next
复制代码 |
|