ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 如何把拆分的表自动建立一个文件夹放入

[复制链接]

TA的精华主题

TA的得分主题

发表于 2021-9-20 09:24 | 显示全部楼层 |阅读模式
每个文件夹的名称就是和文件名一样
Sub 生成明细表()
Application.ScreenUpdating = False
Dim ar As Variant
Dim i As Integer
With Sheets("数据源")
    r = .Cells(Rows.Count, 2).End(xlUp).Row
    If r < 2 Then MsgBox "数据源工作表为空!": End
    ar = .Range("a1:i" & r)
End With
Application.DisplayAlerts = False
For Each sh In Sheets
    If sh.Index > 2 Then
        sh.Delete
    End If
Next sh
Application.DisplayAlerts = True
For i = 2 To UBound(ar)
    If Trim(ar(i, 2)) <> "" Then
        Sheets("模板").Copy after:=Sheets(Sheets.Count)
        With Sheets(Sheets.Count)
            .Name = ar(i, 1) & ar(i, 3)
            .[f4] = ar(i, 2)
            .[c3] = ar(i, 3)
            .[f3] = ar(i, 6)
            .[l3] = ar(i, 5)
            .[c4] = ar(i, 7)
            .[l4] = ar(i, 4)
            .[k5] = ar(i, 9)
            If Trim(ar(i, 8)) = "是" Then
                .CheckBox1.Value = True
                .CheckBox2.Value = False
            ElseIf Trim(ar(i, 8)) = "否" Then
                .CheckBox1.Value = False
                .CheckBox2.Value = True
            End If
        End With
    End If
Next i
Application.ScreenUpdating = True
MsgBox "明细表生成完毕"
End Sub
Sub 拆分明细表()
Application.ScreenUpdating = False
Dim ar As Variant
Dim i As Integer
With Sheets("数据源")
    r = .Cells(Rows.Count, 2).End(xlUp).Row
    If r < 2 Then MsgBox "数据源工作表为空!": End
    ar = .Range("a1:i" & r)
End With
For i = 2 To UBound(ar)
    If Trim(ar(i, 2)) <> "" Then
        mc = ar(i, 1) & ar(i, 3)
        Sheets("模板").Copy
        Set wb = ActiveWorkbook
        With wb.Worksheets(1)
            .Name = mc
            .[f4] = ar(i, 2)
            .[c3] = ar(i, 3)
            .[f3] = ar(i, 6)
            .[l3] = ar(i, 5)
            .[c4] = ar(i, 7)
            .[l4] = ar(i, 4)
            .[k5] = ar(i, 9)
            If Trim(ar(i, 8)) = "是" Then
                .CheckBox2.Value = True
                .CheckBox1.Value = False
            ElseIf Trim(ar(i, 8)) = "否" Then
                .CheckBox2.Value = False
                .CheckBox1.Value = True
            End If
        End With
        wb.SaveAs Filename:=ThisWorkbook.Path & "\" & mc & ".xlsx"
        wb.Close
    End If
Next i
Application.ScreenUpdating = True
MsgBox "明细表拆分为完毕"
End Sub


TA的精华主题

TA的得分主题

发表于 2021-9-20 17:32 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 拆分明细表()
    Application.ScreenUpdating = False
    Dim ar As Variant
    Dim i As Integer
    With Sheets("数据源")
        r = .Cells(Rows.Count, 2).End(xlUp).Row
        If r < 2 Then MsgBox "数据源工作表为空!": End
        ar = .Range("a1:i" & r)
    End With
    For i = 2 To UBound(ar)
        If Trim(ar(i, 2)) <> "" Then
            mc = ar(i, 1) & ar(i, 3)
            Sheets("模板").Copy
            Set wb = ActiveWorkbook
            With wb.Worksheets(1)
                .Name = mc
                .[f4] = ar(i, 2)
                .[c3] = ar(i, 3)
                .[f3] = ar(i, 6)
                .[l3] = ar(i, 5)
                .[c4] = ar(i, 7)
                .[l4] = ar(i, 4)
                .[k5] = ar(i, 9)
                If Trim(ar(i, 8)) = "是" Then
                    .CheckBox2.Value = True
                    .CheckBox1.Value = False
                ElseIf Trim(ar(i, 8)) = "否" Then
                    .CheckBox2.Value = False
                    .CheckBox1.Value = True
                End If
            End With
            If Dir(ThisWorkbook.Path & "\" & mc, 16) = "" Then
                MkDir ThisWorkbook.Path & "\" & mc
            End If
            wb.SaveAs Filename:=ThisWorkbook.Path & "\" & mc & "\" & mc & ".xlsx"
            wb.Close
        End If
    Next i
    Application.ScreenUpdating = True
    MsgBox "明细表拆分为完毕"
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

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

GMT+8, 2024-4-25 12:29 , Processed in 0.027383 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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