|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- PathM = ThisWorkbook.Path & "\模板.xlsx"
- Rem 准备结果文件夹
- PathG = ThisWorkbook.Path & "\拆分结果"
- Set FSO = CreateObject("Scripting.FileSystemObject")
- If FSO.FolderExists(PathG) = True Then
- FSO.GetFolder(PathG).Delete '//删除文件夹
- End If
- MkDir PathG '//创建文件夹
-
- Set SHX = Worksheets("首页")
- Str_coon = "HDR=NO';Data Source =" & ThisWorkbook.FullName '//OFFICE2003,2007 通用
-
- Sql = ""
- For IROW = 2 To SHX.Range("A100").End(3).Row
- If Sql <> "" Then Sql = Sql & " UNION ALL "
- Sql = Sql & " SELECT F" & SHX.Cells(IROW, 4).Value & " AS 省名"
- Sql = Sql & " FROM [" & SHX.Cells(IROW, 1).Value & "$A" & SHX.Cells(IROW, 2).Value + 1 & ":IT]"
- Sql = Sql & " WHERE NOT F" & SHX.Cells(IROW, 4).Value & " IS NULL AND LEN(F" & SHX.Cells(IROW, 4).Value & ")>0"
- Next
-
- Rem 先获取要拆分字段的不重复值
- StrSQL = "SELECT DISTINCT 省名"
- StrSQL = StrSQL & " FROM (" & Sql & ")"
- 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
-
- Set WB = Workbooks.Open(PathM)
- Rem 查询对应数据
- For IROW = 2 To SHX.Range("A100").End(3).Row
- StrSQL = "SELECT * "
- StrSQL = StrSQL & " FROM [" & SHX.Cells(IROW, 1).Value & "$A" & SHX.Cells(IROW, 2).Value + 1 & ":" & SHX.Cells(IROW, 3).Value & "]"
- StrSQL = StrSQL & " WHERE F" & SHX.Cells(IROW, 4).Value & "='" & ARX(X, 0) & "'"
-
- SQLARR = GET_SQL_To_Arr(StrSQL, Str_coon, False)
- If UBound(SQLARR, 1) > 0 Then '//没有数据,在不保存
- Rem 粘贴数据,保存文件
- Set SHW = WB.Worksheets("" & SHX.Cells(IROW, 1).Value)
- SHW.Range("A" & SHX.Cells(IROW, 2).Value + 1).Resize(UBound(SQLARR, 1) + 1, UBound(SQLARR, 2) + 1) = SQLARR
- End If
- Next
- WB.SaveAs Filename:=PathG & "" & ARX(X, 0) & ".XLSX"
- WB.Close True
- Next
- Else
- MsgBox "未发现拆分依据 需要的值!", vbInformation, "北极狐提示!!"
- End If
复制代码
补充内容 (2019-5-30 10:58):
有个bug 只有一行数据的提不出来
修改此句,加个:等号:If UBound(SQLARR, 1) >= 0 Then '//没有数据,在不保存 |
|