http://www.pptjcw.com

excel表格制作:写几段代码,将总表按任意列拆分为多个工作簿

    大家好,我是星光。
    今天和大家分享的VBA小代码是:一键将总表按任意列拆分为多个工作簿

    什么意思呢?举个小栗子。
    如下图所示,是一张总表,现在需要按任意列,比如班级列吧,将它拆分为多个工作簿。

    excel表格制作:写几段代码,将总表按任意列拆分为多个工作簿

    动画演示如下:

    excel表格制作:写几段代码,将总表按任意列拆分为多个工作簿

    VBA代码如下:

    Sub SplitShByArr() Dim shtAct As Worksheet, sht As Worksheet, wb As Workbook Dim rngData As Range, rngGistC As Range, rngTemp As Range Dim d As Object, aData, aKeys, vnt Dim intTitCount, strKey As String, strName As String Dim strADS As String, rngTit As Range Dim i As Long, j As Long, intFirstR As Long, intLastR As Long Dim k As Long, x As Long, intActR As Long Dim intFirstC As Long, intGistC As Long Dim strPath As String On Error Resume Next '忽略错误继续运行程序 ' strPath = getStrPath() '用户选择文件保存路径 If strPath = "" Then Exit Sub ' '获取用户输入的标题行数▼ intTitCount = getTitCount() If intTitCount = False Then Exit Sub ' '获取拆分依据列▼ Set rngGistC = GetRngGistC() If Err.Number Then GoTo errDescript ' Call disAppSet '取消屏幕刷新等系统设置 ' Set shtAct = ActiveSheet '当前工作表 If shtAct.FilterMode = True Then shtAct.Cells.AutoFilter '取消筛选状态 Set rngData = shtAct.UsedRange '实际区域 aData = rngData.Value '总表数据存入数组aData intFirstC = rngData.Column '实际区域开始列 intGistC = rngGistC.Column - intFirstC + 1 '依据列在aData中的序列 intFirstR = rngData.Row '实际区域开始行 intActR = intTitCount - intFirstR + 2 '扣除标题的数组开始行 intLastR = GetintLastR(shtAct) '实际区域结束行 With shtAct '标题区域 Set rngTit = .Range(.Cells(1, 1), _ .Cells(intTitCount, _ UBound(aData, 2) + intFirstC - 1)) End With ' '参数数组,修正异常数据▼ Set d = CreateObject("scripting.dictionary") '后期字典 ReDim aRef(1 To intLastR) '数组aRef,修正拆分列特殊数据 For i = intActR To UBound(aData) If i > intLastR Then Exit For '如果大于有效数据最大行则退出循环 vnt = aData(i, intGistC) If IsError(vnt) Then aRef(i) = "错误值" ElseIf vnt = "" Then aRef(i) = "空白单元格" ElseIf IsDate(vnt) Then '避免日期斜杠格式无法创建工作簿/表 aRef(i) = Format(vnt, "yyyy-m-d") Else aRef(i) = vnt End If strKey = aRef(i) d(strKey) = d(strKey) + 1 '记录不同拆分关键字的数量 Next ' '通过前8行数据来判断该列是否为特殊的文本数值 For j = 1 To UBound(aData, 2) '遍历列 For i = intActR To UBound(aData) '遍历前8行 If i > 8 Then Exit For vnt = aData(i, j) If IsNumeric(vnt) Then '是否数值 If VarType(aData(i, j)) = 8 Then '是否文本 strADS = strADS & "," & Cells(1, j + intFirstC - 1).Address Exit For End If End If Next Next strADS = Mid(strADS, 2) '需要设置文本格式的单元格地址 ' aKeys = d.keys '字典Keys,拆分关键字数组 For i = 0 To UBound(aKeys) '遍历关键字 strName = aKeys(i) '关键字 ReDim aRes(1 To d(strName), 1 To UBound(aData, 2)) '结果数组 k = 0 '计数器归0 ' '筛选符合条件的记录存入结果数组 For x = 1 To UBound(aRef) If aRef(x) = strName Then '如果关键字符合 k = k + 1 '累加符合条件的行 For j = 1 To UBound(aData, 2) '遍历列 aRes(k, j) = aData(x, j) '数据存入结果数组 Next End If Next ' '建立新工作簿,存放结果数组 Set wb = Workbooks.Add With wb.Worksheets(1) .Name = strName '命名 If Err.Number Then '如果名称有特殊字符,则退出程序 .Delete wb.Close False GoTo errDescript End If If Len(strADS) Then .Range(strADS).EntireColumn.NumberFormat = "@" '特殊列设置为文本格式 End If With .Cells(intTitCount + 1, intFirstC).Resize(k, UBound(aRes, 2)) .Value = aRes '结果数组数据写入工作表 End With .UsedRange.Borders.LineStyle = 1 '设置边框线 rngTit.Copy .Range("a1").PasteSpecial xlPasteColumnWidths '粘贴列宽 .Range("a1").PasteSpecial xlPasteAll '粘贴标题 End With wb.SaveAs strPath & strName wb.Close False Next errDescript: shtAct.Select Call reAppSet '恢复屏幕刷新等系统设置 Set d = Nothing '释放字典内存 If Err.Number Then MsgBox Err.Description Else MsgBox "拆分完成。" End If End Sub '用户选择文件夹路径 Function getStrPath() As String Dim strPath As String With Application.FileDialog(msoFileDialogFolderPicker) If .Show Then strPath = .SelectedItems(1) Else '如用户为选中文件夹则退出 Exit Function End If End With If Right(strPath, 1) <> "\" Then strPath = strPath & "\" getStrPath = strPath End Function '获取用户输入的标题行数 Function getTitCount() Dim intTitCount intTitCount = InputBox("请输入标题行的行数", _ Title:="公众号Excel星球", _ Default:=1) If StrPtr(intTitCount) = False Then getTitCount = False Exit Function End If If IsNumeric(intTitCount) = False Then MsgBox "标题行的行数只能输入数字。" getTitCount = False Exit Function End If If intTitCount < 0 Then MsgBox "标题行数不能为负数。" getTitCount = False Exit Function End If getTitCount = intTitCount End Function '用户选择拆分依据列 Function GetRngGistC() As Range Dim rngGistC As Range Set rngGistC = Application.InputBox("请选择拆分依据列。", _ Title:="公众号Excel星球", _ Default:=Selection.Address, _ Type:=8) If rngGistC Is Nothing Then Exit Function End If If rngGistC.Columns.Count > 1 Then MsgBox "拆分依据列只能是单列。" Exit Function End If Set GetRngGistC = rngGistC End Function '取消屏幕刷新,公式重算等 Sub disAppSet() With Application .ScreenUpdating = False .Calculation = xlCalculationManual .DisplayAlerts = False End With End Sub '恢复屏幕刷新等 Sub reAppSet() With Application .ScreenUpdating = True .Calculation = xlCalculationAutomatic .DisplayAlerts = True End With End Sub '最大数据有效行 Function GetintLastR(ByVal sht As Worksheet) GetintLastR = sht.Cells.Find("*", _ LookIn:=xlFormulas, SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious).Row End Function

    代码详细解释见注释,概要说明如下▼

    第13至第14行代码,调用getStrPath函数过程,打开文件浏览对话框,允许用户选择任意文件夹作为数据源;如果用户未选取文件夹,则退出程序。

    第17至第18行代码,调用getTitCount过程,允许用户输入指定行数的标题行。

    第21至第22行代码,调用GetRngGistC过程,允许用户选择拆分依据列。

    第24行代码,调用disAppSet过程,取消屏幕刷新等系统设置。

    提示:如果您觉得本文不错,请点击分享给您的好友!谢谢

    上一篇:xcel表格制作教程入门:练手题,来试试 下一篇:excel怎么做表格:你绝对想不到,用ChatGPT可以从Excel表里面的几百条留言快速得到结论

    郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。