前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ExcelVBA拆分之一簿一表_to_一簿多表使用演示

ExcelVBA拆分之一簿一表_to_一簿多表使用演示

作者头像
哆哆Excel
发布2023-09-09 10:50:52
2430
发布2023-09-09 10:50:52
举报
文章被收录于专栏:哆哆Excel
  1. ExcelVBA拆分之一簿一表_to_一簿多表
  2. ExcelVBA汇总-多簿一表_to_一簿一表
  3. ExcelVBA汇总多工作簿中指定工作表到新工作簿
  4. ExceVBA删除指定字符所在的行_优化版
  5. ExcelVBA选择文件夹(含子文件夹)获取所有文件列表
  6. ExcelVBA删除指定列含有指定字符的所在的行
  7. ExcelVBA删除包含指定字符所在的行
  8. 在Excel中通过VBA对Word文档进行查找替换
  9. ExcelVBA文件操作-获取文件夹(含子文件夹)所有文件列表(优化版)
  10. ExcelVBA随机生成不重复的N个N位数文本
代码语言:javascript
复制
    '把当前表拆分:一簿一表_to_一簿多表
    '作者:哆哆
    '时间:2023-05
Sub yhd_ExcelVBA_3拆分_一簿一表_to_一簿多表()
    Dim title_row As Integer, RngCol As Range, split_Col As Integer
    Dim dic As Object, ThisSht As Worksheet, i As Long, pathstr As String
    Dim newflag As Boolean, ThisWb As Workbook
    Set dic = CreateObject("scripting.dictionary")
    disAppSet (False)
    On Error Resume Next
    title_row = Application.InputBox(prompt:="请输入标题行数:", Type:=1)
    Set RngCol = Application.InputBox(prompt:="请选择", Default:=Selection.Address, Title:="选择", Type:=8)
    If title_row = False Or RngCol = False Or title_row < 1 Then MsgBox "输入有误或选择空白区域,退了", 16, "哆哆提示": Exit Sub
    On Error GoTo 0                                            '以下恢复捕捉代码出现错误消息
    t = Timer
    split_Col = RngCol.Column
    pathstr = ThisWorkbook.Path & "\"
    Set ThisSht = ActiveSheet
    With ThisSht
        lastrow = .Cells.Find("*", , , , 1, 2).Row
        For i = title_row + 1 To lastrow
            s = Trim(.Cells(i, split_Col))
            If s <> "" Then
                dic(s) = IIf(dic.exists(s), dic(s) & "_" & i, i)
            End If
        Next i
    End With
    newflag = True
    For j = 0 To dic.Count - 1
        If newflag Then
            Set ThisWb = Workbooks.Add
        Else
            Set ThisWb = ThisWorkbook
        End If
        Set addSht = ThisWb.Worksheets.Add(After:=ThisWb.Worksheets(ThisWb.Worksheets.Count))
        With addSht
            ThisSht.Cells(1, 1).Resize(title_row, 1).EntireRow.Copy .Cells(1, 1)
            cc = VBA.Split(dic.items()(j), "_")
            Set ran = ThisSht.Rows(cc(0))
            For i = 1 To UBound(cc)
                If cc(i) <> "" Then
                    Set ran = Application.Union(ran, ThisSht.Rows(cc(i)))
                End If
            Next i
            ran.Copy
            .Cells(title_row + 1, 1).Select
            Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats
            Selection.PasteSpecial Paste:=xlPasteFormats
            For Each shp In .Shapes
                shp.Delete
            Next shp
            .Cells(1, 1).Select
            .Name = dic.keys()(j)
        End With
        If newflag Then
            ThisWb.SaveAs pathstr & dic.keys()(j)
            ThisWb.Close True
        Else
            ThisWb.Save
        End If
        Set ThisWb = Nothing
    Next j
    MsgBox "拆分" & dic.Count & "个,用时:" & Format(Timer - t, "0.00秒")
    disAppSet (True)
End Sub
    '用法:disAppSet(true)开disAppSet(true)关
Sub disAppSet(flag As Boolean)
    With Application
        .ScreenUpdating = flag
        .DisplayAlerts = flag
        .AskToUpdateLinks = flag
        If flag Then
            .Calculation = xlCalculationAutomatic
        Else
            .Calculation = xlCalculationManual
        End If
    End With
End Sub
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-05-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 哆哆Excel 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档