|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Set SHX = Worksheets("汇总")
- PathG = ThisWorkbook.Path & "\拆分结果" '//结果文件夹
- Set FSO = CreateObject("Scripting.FileSystemObject")
- If FSO.FolderExists(PathG) = True Then
- FSO.GetFolder(PathG).Delete '//删除文件夹
- End If
- MkDir PathG '//创建文件夹
- Rem 模板中放置数据的单元格位置,和查询标题对应
- StrBT = ""
- For ICOL = 1 To SHX.Range("IT1").End(xlToLeft).Column
- If StrBT <> "" Then StrBT = StrBT & ","
- StrBT = StrBT & "[" & SHX.Cells(1, ICOL).Value & "]"
- Next
-
- Str_coon = "HDR=yes';Data Source =" & ThisWorkbook.FullName '//OFFICE2003,2007 通用
- Rem 先获取要拆分字段的不重复值
- StrSQL = "SELECT DISTINCT [部门]"
- StrSQL = StrSQL & " FROM [" & SHX.Name & "$A1:IT]"
- StrSQL = StrSQL & " WHERE NOT [部门] IS NULL AND LEN([部门])>0"
-
- ARX = GET_SQL_To_Arr(StrSQL, Str_coon, False) '//不重复姓名放入二维数组
- If ARX(0, 0) <> "" And ARX(0, 0) <> "Error" Then
- ICINT = UBound(ARX) + 1
- For X = 0 To ICINT - 1 '//循环每一个值
-
- Rem 提示信息,在状态栏显示
- Application.StatusBar = "需拆分总数:" & ICINT & " 个,当前是第:" & X + 1 & " 个,当前部门是:" & ARX(X, 0)
- DoEvents
-
- Rem 查询对应数据
- StrSQL = ""
- StrSQL = StrSQL & "SELECT " & StrBT
- StrSQL = StrSQL & " FROM [" & SHX.Name & "$A1:IT]"
- StrSQL = StrSQL & " WHERE [部门]='" & ARX(X, 0) & "'"
- SQLARR = GET_SQL_To_Arr(StrSQL, Str_coon, True)
- If SQLARR(0, 0) <> "" And SQLARR(0, 0) <> "Error" Then '//没有数据,在不保存
- Set WB = Workbooks.Add
- Set SHW = Worksheets(1)
- SHW.Name = "汇总"
- SHW.Range("A1").Resize(UBound(SQLARR, 1) + 1, UBound(SQLARR, 2) + 1) = SQLARR
- WB.SaveAs Filename:=PathG & "" & ARX(X, 0) & ".XLS", FileFormat:=xlExcel8
- WB.Close True
- End If
- Next
- Else
- MsgBox "未发现拆分依据 需要的值!", vbInformation, "北极狐提示!!"
- End If
复制代码 |
评分
-
1
查看全部评分
-
|