ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 214|回复: 6

[求助] 搞定了 请斑竹关闭

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-5-6 11:56 | 显示全部楼层 |阅读模式
本帖最后由 fallrains 于 2024-5-6 13:57 编辑

请斑竹关闭 RT
image.png

工作簿1.zip

12.6 KB, 下载次数: 11

TA的精华主题

TA的得分主题

发表于 2024-5-6 12:05 | 显示全部楼层
听不懂,哪位听懂了?

TA的精华主题

TA的得分主题

发表于 2024-5-6 12:42 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. Option Explicit

  2. Sub test0() '现成的,改了两处 关键词 branch
  3.   Dim data, wks() As String, Flag() As Boolean
  4.   Dim Ran As Range, Target As Range, Dict As Object, vrtKey
  5.   Dim strPath As String, splitKey As String, strKey As String
  6.   Dim i As Long, j As Long
  7.   DoApp False
  8.   strPath = ThisWorkbook.Path & "\分簿\"
  9.   If Dir(strPath, vbDirectory) = "" Then MkDir strPath
  10.   'strPath = strPath & "\"
  11.   Set Dict = CreateObject("Scripting.Dictionary")
  12.   j = Worksheets.Count
  13.   ReDim wks(1 To j), Flag(1 To j)
  14.   splitKey = "branch"
  15.   For j = 1 To UBound(wks)
  16.     With Worksheets(j)
  17.       Set Target = .UsedRange.Find(splitKey, , xlValues, , xlByRows, xlPrevious)
  18.       If Not Target Is Nothing Then
  19.         Flag(j) = True
  20.         data = .Cells(1, Target.Column).Resize(.Cells(.Rows.Count, Target.Column).End(xlUp).Row)
  21.         For i = Target.Row + Target.MergeArea.Rows.Count To UBound(data)
  22.           strKey = Trim(data(i, 1))
  23.           If Len(strKey) Then If Not Dict.Exists(strKey) Then Dict.Add strKey, vbNullString
  24.         Next
  25.       End If
  26.       wks(j) = .Name
  27.     End With
  28.   Next
  29.   For Each vrtKey In Dict.Keys
  30.     Worksheets(wks).Copy
  31.     With ActiveWorkbook
  32.       For j = 1 To UBound(wks)
  33.         If Flag(j) Then
  34.           With .Worksheets(wks(j))
  35.             .DrawingObjects.Delete
  36.             Set Ran = .Rows(.Rows.Count)
  37.             Set Target = .UsedRange.Find(splitKey, , xlValues, , xlByRows, xlPrevious)
  38.             data = .Cells(1, Target.Column).Resize(.Cells(.Rows.Count, Target.Column).End(xlUp).Row)
  39.             For i = Target.Row + Target.MergeArea.Rows.Count To UBound(data)
  40.               If Trim(data(i, 1)) <> vrtKey Then Set Ran = Union(Ran, .Rows(i))
  41.             Next
  42.             Ran.Delete
  43.           End With
  44.         End If
  45.       Next
  46.       .SaveAs strPath & vrtKey & IIf(InStr(vrtKey, "."), ".xlsx", vbNullString), 51
  47.       .Close
  48.     End With
  49.   Next

  50.   Set Dict = Nothing
  51.   Set Target = Nothing
  52.   Set Ran = Nothing
  53.   DoApp
  54.   Beep
  55. End Sub

  56. Sub test1() '另一法
  57.   Dim data, wks() As String, SQL(), pos() As Long, Flag() As Boolean
  58.   Dim Conn As Object, Dict As Object, Target As Range, vrtKey
  59.   Dim strPath As String, splitKey As String, strKey As String
  60.   Dim i As Long, j As Long
  61.   DoApp False
  62.   strPath = ThisWorkbook.Path & "\分簿\"
  63.   If Dir(strPath, vbDirectory) = "" Then MkDir strPath
  64.   'strPath = strPath & "\"
  65.   Set Dict = CreateObject("Scripting.Dictionary")
  66.   j = Worksheets.Count
  67.   ReDim wks(1 To j), SQL(1 To j), pos(1 To j), Flag(1 To j)
  68.   splitKey = "branch"
  69.   For j = 1 To UBound(wks)
  70.     With Worksheets(j)
  71.       Set Target = .UsedRange.Find(splitKey, , xlValues, , xlByRows, xlPrevious)
  72.       'wks(j) = .Name
  73.       If Not Target Is Nothing Then
  74.         Flag(j) = True
  75.         data = .Cells(1, Target.Column).Resize(.Cells(.Rows.Count, Target.Column).End(xlUp).Row)
  76.         pos(j) = Target.Row + Target.MergeArea.Rows.Count
  77.         For i = pos(j) To UBound(data)
  78.           strKey = Trim(data(i, 1))
  79.           If Len(strKey) Then If Not Dict.Exists(strKey) Then Dict.Add strKey, vbNullString
  80.         Next
  81.         wks(j) = .Name
  82.         With .Range("A1").CurrentRegion
  83.           SQL(j) = "SELECT * FROM [" & wks(j) & "$" & Intersect(.Offset(0), .Offset(pos(j) - 1)).Address(0, 0) & "] WHERE TRIM(F" & Target.Column & ")="
  84.         End With
  85.       End If
  86.     End With
  87.   Next
  88.   Set Target = Nothing
  89.   Set Conn = CreateObject("ADODB.Connection")
  90.   Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=NO';Data Source=" & ThisWorkbook.FullName
  91.   For Each vrtKey In Dict.Keys
  92.     Worksheets(wks).Copy
  93.     With ActiveWorkbook
  94.       For j = 1 To UBound(wks)
  95.         If Flag(j) Then
  96.           With .Worksheets(wks(j))
  97.             .DrawingObjects.Delete
  98.             .UsedRange.Offset(pos(j) - 1).ClearContents
  99.             .Cells(pos(j), "A").CopyFromRecordset Conn.Execute(SQL(j) & "'" & vrtKey & "'")
  100.           End With
  101.         End If
  102.       Next
  103.       .SaveAs strPath & vrtKey, 51
  104.       .Close
  105.     End With
  106.   Next
  107.   Conn.Close
  108.   Set Conn = Nothing
  109.   Set Dict = Nothing
  110.   DoApp True
  111.   Beep
  112. End Sub

  113. Function DoApp(Optional b As Boolean = True)
  114.   With Application
  115.     .ScreenUpdating = b
  116.     .DisplayAlerts = b
  117.     .Calculation = -b * 30 - 4135
  118.   End With
  119. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2024-5-6 12:51 | 显示全部楼层
感谢分享,祝您生活愉快,再见

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-5-6 13:24 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

这个应该只有拆分吧,合并用哪个呢

TA的精华主题

TA的得分主题

发表于 2024-5-6 14:11 | 显示全部楼层
本帖最后由 baofa2 于 2024-5-6 14:19 编辑
fallrains 发表于 2024-5-6 13:24
这个应该只有拆分吧,合并用哪个呢
  1. Sub test2() '据提供作表汇总,首行标题预先自拟,工作表无 则添加。能写这些,是不易的……请先谢人!
  2.   
  3.   Dim path_ As String, files_() As String
  4.   path_ = ThisWorkbook.Path & "\分簿\"
  5.   If Not GetFileName(files_, path_, ".xlsx") Then MsgBox "!": Exit Sub
  6.   
  7.   DoApp False
  8.   
  9.   Dim Conn As Object, rs As Object, Cata As Object, tb As Object, dict As Object
  10.   Dim strConn As String, SQL As String, s As String, t As String
  11.   Dim i As Long, j As Long, iCount As Long, pos() As Long, vKey
  12.   
  13.   Set rs = CreateObject("ADODB.Recordset")
  14.   Set Conn = CreateObject("ADODB.Connection")
  15.   Set Cata = CreateObject("ADOX.Catalog")
  16.   Set dict = CreateObject("Scripting.Dictionary")
  17.   
  18.   ReDim pos(1 To Worksheets.Count)
  19.   For i = 1 To Worksheets.Count
  20.     pos(i) = 1
  21.     With Worksheets(i)
  22.       .UsedRange.Offset(pos(i)).ClearContents
  23.       Set dict(.Name & "$") = CreateObject("Scripting.Dictionary")
  24.     End With
  25.   Next
  26.   
  27.   s = "Excel 12.0;HDR=YES;Database="
  28.   If Application.Version < 12 Then
  29.     s = Replace(s, "12.0", "8.0")
  30.     strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=YES';Data Source="
  31.   Else
  32.     strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';Data Source="
  33.   End If
  34.   Conn.Open strConn & ThisWorkbook.FullName
  35.   
  36.   For i = LBound(files_) To UBound(files_)
  37.     SQL = "SELECT * FROM [" & s & files_(i) & "].[[t$]] WHERE LEN(`branch`)"
  38.     Cata.ActiveConnection = strConn & files_(i)
  39.     For Each tb In Cata.Tables
  40.       If tb.Type = "TABLE" Then
  41.         t = Replace(tb.Name, "'", vbNullString)
  42.         If Right(t, 1) = "$" Then
  43.           If Not dict.Exists(t) Then
  44.             Set dict(t) = CreateObject("Scripting.Dictionary")
  45.             Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = Left(t, Len(t) - 1)
  46.             ReDim Preserve pos(1 To Worksheets.Count)
  47.             pos(UBound(pos)) = 1
  48.           End If
  49.           dict(t).Add Replace(SQL, "[t$]", t), vbNullString
  50.         End If
  51.       End If
  52.     Next
  53.     iCount = iCount + 1
  54.     If iCount = 49 Then
  55.       For Each vKey In dict.Keys
  56.         If dict(vKey).Count Then
  57.           Set rs = Conn.Execute(Join(dict(vKey).Keys, " UNION ALL "))
  58.           With Worksheets(Left(vKey, Len(vKey) - 1))
  59.             .Cells(pos(.Index) + 1, "A").CopyFromRecordset rs
  60.             pos(.Index) = .Cells.Find("*", , , , 1, 2).Row
  61.           End With
  62.           dict(vKey).RemoveAll
  63.         End If
  64.       Next
  65.       iCount = 0
  66.     End If
  67.   Next
  68.   
  69.   If iCount > 0 Then
  70.     For Each vKey In dict.Keys
  71.       If dict(vKey).Count Then
  72.         Set rs = Conn.Execute(Join(dict(vKey).Keys, " UNION ALL "))
  73.         With Worksheets(Left(vKey, Len(vKey) - 1))
  74.           .Cells(pos(.Index) + 1, "A").CopyFromRecordset rs
  75.         End With
  76.       End If
  77.       Set dict(vKey) = Nothing
  78.     Next
  79.   End If
  80.   
  81.   Set Cata = Nothing
  82.   rs.Close
  83.   Set rs = Nothing
  84.   Conn.Close
  85.   Set Conn = Nothing
  86.   Set dict = Nothing
  87.   
  88.   DoApp
  89.   Beep
  90. End Sub

  91. Function GetFileName(files_, path_ As String, ext As String) As Boolean
  92.   Dim idx As Long, cnt As Long
  93.   Dim strDir As String, Folders_(9999) As String
  94.   If Right(path_, 1) <> "\" Then path_ = path_ & "\"
  95.   Do
  96.     strDir = Dir(path_, vbDirectory)
  97.     Do
  98.       If strDir <> "." And strDir <> ".." Then
  99.         If (GetAttr(path_ & strDir) And vbDirectory) = vbDirectory Then
  100.           cnt = cnt + 1
  101.           Folders_(cnt) = path_ & strDir & "\"
  102.           Debug.Print Folders_(cnt)
  103.         Else
  104.           If LCase(Right(strDir, Len(ext))) = LCase(ext) Then '限于指定的文件扩展名
  105.             idx = idx + 1
  106.             ReDim Preserve files_(1 To idx)
  107.             files_(idx) = path_ & strDir
  108.           End If
  109.         End If
  110.       End If
  111.       strDir = Dir
  112.     Loop While Len(strDir)
  113.     If cnt = 0 Then Exit Do
  114.     path_ = Folders_(cnt)
  115.     cnt = cnt - 1
  116.   Loop
  117.   GetFileName = CBool(idx)
  118. End Function

  119. Function DoApp(Optional b As Boolean = True)
  120.   With Application
  121.     .ScreenUpdating = b
  122.     .DisplayAlerts = b
  123.     .Calculation = -b * 30 - 4135
  124.   End With
  125. End Function
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-5-9 10:47 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-19 16:16 , Processed in 0.042614 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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