ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[已解决] 生成新的工作簿

[复制链接]

TA的精华主题

TA的得分主题

发表于 2022-11-1 11:05 | 显示全部楼层
Sub 拆分()
Application.ScreenUpdating = False
Dim ar As Variant
Dim sh As Worksheet
With Sheets("数据源")
    r = .Cells(Rows.Count, 1).End(xlUp).Row
    ar = .Range("a1:d" & r)
    rs = .Cells(Rows.Count, 7).End(xlUp).Row
    br = .Range("g1:i" & rs)
End With
w = MsgBox("拆分为工作表选择是,拆分为工作簿选择否", vbYesNo)
If w = "" Then End
Application.DisplayAlerts = False
If w = vbYes Then
    For Each sht In Sheets
        If sht.Index > 2 Then
            sht.Delete
        End If
    Next sht
End If
Application.DisplayAlerts = True
Set sh = ThisWorkbook.Worksheets("模板")
For i = 2 To UBound(br)
    n = 0
    ReDim arr(1 To UBound(ar), 1 To 1)
    If Trim(br(i, 1)) <> "" And Trim(br(i, 2)) <> "" Then
        zd = Trim(br(i, 1)) & Trim(br(i, 2))
        For s = 2 To UBound(ar)
            If Trim(ar(s, 1)) <> "" And Trim(ar(s, 2)) <> "" Then
                zf = Trim(ar(s, 1)) & Trim(ar(s, 2))
                If zf = zd Then
                    n = n + 1
                    arr(n, 1) = ar(s, 4)
                End If
            End If
        Next s
        If n > 0 Then
            If w = vbYes Then
                sh.Copy after:=Sheets(Sheets.Count)
                With Sheets(Sheets.Count)
                    .Name = zd
                    .[a1] = "某某学校" & zd & "班学生体温监测表" & Chr(10) & "日期:                      班主任:" & br(i, 3)
                    If n <= 30 Then
                        .[b3].Resize(n, 1) = arr
                    ElseIf n > 30 Then
                        If n / 2 = Int(n / 2) Then
                            sl = n / 2
                        Else
                            sl = Int(n / 2) + 1
                        End If
                        m = 0
                        For s = 1 To n Step sl
                            m = m + 1
                            If m = 1 Then
                                lh = 2
                            Else
                                lh = 8
                            End If
                            hh = 2
                            For ss = s To s + sl - 1
                                If ss <= n Then
                                    hh = hh + 1
                                    .Cells(hh, lh) = arr(ss, 1)
                                End If
                            Next ss
                        Next s
                    End If
                End With
            ElseIf w = vbNo Then
                sh.Copy
                Set wb = ActiveWorkbook
                With wb.Worksheets(1)
                    .Name = zd
                    .[a1] = "某某学校" & zd & "班学生体温监测表" & Chr(10) & "日期:                      班主任:" & br(i, 3)
                    If n <= 30 Then
                        .[b3].Resize(n, 1) = arr
                    ElseIf n > 30 Then
                        If n / 2 = Int(n / 2) Then
                            sl = n / 2
                        Else
                            sl = Int(n / 2) + 1
                        End If
                        m = 0
                        For s = 1 To n Step sl
                            m = m + 1
                            If m = 1 Then
                                lh = 2
                            Else
                                lh = 8
                            End If
                            hh = 2
                            For ss = s To s + sl - 1
                                If ss <= n Then
                                    hh = hh + 1
                                    .Cells(hh, lh) = arr(ss, 1)
                                End If
                            Next ss
                        Next s
                    End If
                End With
                wb.SaveAs Filename:=ThisWorkbook.Path & "\" & zd & ".xlsx"
                wb.Close
            End If
        End If
    End If
Next i
Application.ScreenUpdating = True
MsgBox "拆分完毕!", 64, "提醒!"
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2022-11-1 11:07 | 显示全部楼层
三项需求合并为一个按钮
生成新工作簿.rar (29.12 KB, 下载次数: 23)

评分

2

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2022-11-1 17:31 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
3190496160 发表于 2022-11-1 11:07
三项需求合并为一个按钮

我运行了 代码没问题 但是格式上有些问题。

1.某某学校年级班学生体温监测表

2.按照30人一组排,不需要将班级人数一分为2排

3.第59号下面那里  班主任的姓名没有填充。
微信图片_20221101172651.jpg
微信图片_20221101172703.jpg

TA的精华主题

TA的得分主题

发表于 2022-11-2 08:43 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
生成新工作簿.rar (35.46 KB, 下载次数: 12)

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2022-11-2 09:34 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 熊文来 于 2022-11-2 09:49 编辑

感谢您的帮助。


1.我刚刚运行了下新的文件,发现生成的工作表 标题行中 从10班-14班这些班级的不对。

2、还是标题行里。年级和班级的下划线没有添加。


3.还有个小小的要求看能不能再修改下。就是标题行上面一行某某学校这里开始字号大一些,下一行日期这里还是 字号小一些 可以修改吗?


微信图片_20221102094349.jpg
微信图片_20221102094459.jpg

TA的精华主题

TA的得分主题

发表于 2022-11-6 09:48 | 显示全部楼层
Sub 拆分()
Application.ScreenUpdating = False
Dim ar As Variant
Dim sh As Worksheet
With Sheets("数据源")
    r = .Cells(Rows.Count, 1).End(xlUp).Row
    ar = .Range("a1:d" & r)
    rs = .Cells(Rows.Count, 7).End(xlUp).Row
    br = .Range("g1:i" & rs)
End With
w = MsgBox("拆分为工作表选择是,拆分为工作簿选择否", vbYesNo)
If w = "" Then End
Application.DisplayAlerts = False
If w = vbYes Then
    For Each sht In Sheets
        If sht.Index > 2 Then
            sht.Delete
        End If
    Next sht
End If
Application.DisplayAlerts = True
Set sh = ThisWorkbook.Worksheets("模板")
For i = 2 To UBound(br)
    n = 0
    ReDim arr(1 To UBound(ar), 1 To 1)
    If Trim(br(i, 1)) <> "" And Trim(br(i, 2)) <> "" Then
        zd = Trim(br(i, 1)) & "|" & Trim(br(i, 2))
        For s = 2 To UBound(ar)
            If Trim(ar(s, 1)) <> "" And Trim(ar(s, 2)) <> "" Then
                zf = Trim(ar(s, 1)) & "|" & Trim(ar(s, 2))
                If zf = zd Then
                    n = n + 1
                    arr(n, 1) = ar(s, 4)
                End If
            End If
        Next s
        If n > 0 Then
            rr = Split(zd, "|")
            If w = vbYes Then
                sh.Copy after:=Sheets(Sheets.Count)
                With Sheets(Sheets.Count)
                    .Name = Replace(zd, "|", "")
                    .[a1] = "某某学校" & rr(0) & "年级" & rr(1) & "班学生体温监测表" & Chr(10) & "日期:                      班主任:" & br(i, 3)
                    If n <= 30 Then
                        .[b4].Resize(n, 1) = arr
                    ElseIf n > 30 Then
                        hh = 3
                        lh = 2
                        For s = 1 To 30
                            hh = hh + 1
                            .Cells(hh, lh) = arr(s, 1)
                        Next s
                        hh = 3
                        lh = 8
                        For s = 31 To n
                            hh = hh + 1
                            .Cells(hh, lh) = arr(s, 1)
                        Next s
                    End If
                    .[h33] = br(i, 3)
                    '.PrintOut
                End With
            ElseIf w = vbNo Then
                sh.Copy
                Set wb = ActiveWorkbook
                With wb.Worksheets(1)
                    .Name = Replace(zd, "|", "")
                    .[a1] = "某某学校" & rr(0) & "年级" & rr(1) & "班学生体温监测表" & Chr(10) & "日期:                      班主任:" & br(i, 3)
                    If n <= 30 Then
                        .[b4].Resize(n, 1) = arr
                    ElseIf n > 30 Then
                        hh = 3
                        lh = 2
                        For s = 1 To 30
                            hh = hh + 1
                            .Cells(hh, lh) = arr(s, 1)
                        Next s
                        hh = 3
                        lh = 8
                        For s = 31 To n
                            hh = hh + 1
                            .Cells(hh, lh) = arr(s, 1)
                        Next s
                    End If
                    .[h33] = br(i, 3)
                End With
                wb.SaveAs Filename:=ThisWorkbook.Path & "\" & Replace(zd, "|", "") & ".xlsx"
                wb.Close
            End If
        End If
    End If
Next i
Application.ScreenUpdating = True
MsgBox "拆分完毕!", 64, "提醒!"
End Sub

TA的精华主题

TA的得分主题

发表于 2022-11-6 09:49 | 显示全部楼层
什么下划线纯粹是无聊的游戏,华为不是的东西,没必要
生成新工作簿.rar (35.64 KB, 下载次数: 23)

评分

3

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2022-11-6 17:09 | 显示全部楼层
3190496160 发表于 2022-11-6 09:49
什么下划线纯粹是无聊的游戏,华为不是的东西,没必要

感谢你的帮助 谢谢!

TA的精华主题

TA的得分主题

发表于 2022-11-7 09:05 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 少如 于 2022-11-7 09:10 编辑
3190496160 发表于 2022-11-6 09:49
什么下划线纯粹是无聊的游戏,华为不是的东西,没必要

3190496160老师:
很好用!点赞!!!
拆分生成新工作簿.zip (85.94 KB, 下载次数: 15)

TA的精华主题

TA的得分主题

发表于 2022-11-7 09:15 | 显示全部楼层
3190496160 发表于 2022-11-6 09:49
什么下划线纯粹是无聊的游戏,华为不是的东西,没必要

如果将新生成的工作薄都放入一个文件夹里就更完美了。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-6-26 12:53 , Processed in 0.049128 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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