Excel VBA 将工作表按列拆分且生成新工作表(同个工作簿内)

需求:现在有一张工作表,需要按业务员姓名分成小的工作表,放在同一个工作簿里。

源表格:

效果:

Sub splitSht()
Dim sht As Worksheet
Dim D As Object
Dim j As Integer
Dim t
Dim SourceSht, KeyColumn As String
    
    SourceSht = "销售金额" '源sheet表,即总的明细表sheet名称,命名为SourceSht
    KeyColumn = "B"  '关键列,业务员所在列,即拆分数据的依据,命名为KeyColumn
    
    t = Timer  '获取程序开始前的时间
    Set sht = ThisWorkbook.Worksheets(Sheet1)
    Set D = CreateObject("scripting.dictionary")  '创建一个字典,命名为D
    With sht
        rrow = .Range(KeyColumn & "65535").End(xlUp).Row '从关键列的最大行数65535往上数,获取有数据的最后一行的行数
        For i = 2 To rrow  '从第二行到最后一行,循环进行
            strr = .Range(KeyColumn & i).Value  '获取关键列的当前行所在单元格的数值,即当前业务员的名字,命名为strr
            If Not D.exists(strr) Then  '判断当前业务员strr是否已经在字典D中,如果不在,则
                D.Add strr, .Range("A1").Resize(1, 30)  '第一步,将A1所在行(即表头)添加进字典D中。.Range("A1").Resize(1, 30)指以A1为基准向下向右获取数据,向下获取1行(含本行),向右获取30列(含本列)的数据
                Set D.Item(strr) = Union(D.Item(strr), .Range("A" & i).Resize(1, 30))  '第二步,将业务员名字(键)对应的信息(值)更新为表头和该业务员strr所在行的合集,即添加业务员所在行
            Else
                Set D.Item(strr) = Union(D.Item(strr), .Range("A" & i).Resize(1, 30)) '如果strr已在字典D中,则不需要第一步添加表头,直接进行第二步添加该业务员名字strr所在行
            End If
        Next
        k = D.keys '键=业务员名字
        i = D.items '值=所有含有该业务员名字的行的合集
        For j = 0 To D.Count - 1
            ThisWorkbook.Worksheets.Add after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)  '新建一个sheet表,建在已有的sheet表后面
            ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count).Name = k(j) '将新sheet表命名为业务员名字
            i(j).Copy Worksheets(k(j)).Range("A1")  '将字典中的内容复制粘贴到业务员sheet表里,从A1开始粘贴
            Worksheets(k(j)).Cells.EntireColumn.AutoFit  ''调整粘贴后的格式,自适应列宽
        Next
    End With
    MsgBox "程序运行完成,共耗时:" & Timer - t & "秒"  '用程序结束后的时间减掉开始前的时间,获取程序运行耗时,并弹出输出框显示
    
End Sub

1. 每一行都添加了注释,因为是帮同事写的,便于不懂任何代码的人理解。我自己也是小白,上面的代码是参考别人的代码改出来的,详见链接:https://blog.csdn.net/q215046120/article/details/121023362#:~:text=Vba%20%E4%B8%80%E5%BC%A0%E5%B7%A5%E4%BD%9C%E8%A1%A8%E6%8B%86%E5%88%86%E5%A4%9A%E5%BC%A0%E5%B7%A5%E4%BD%9C%E8%A1%A8%201%20Sub%20splitSht%20%28%29%202%20Dim,%3D%20.Range%20%28%20%22A65535%22%20%29.%20End%20%28xlUp%29.Row%20%E6%9B%B4%E5%A4%9A%E9%A1%B9%E7%9B%AE

2.额外写了计算程序运行时间的代码,不需要的可以删掉。

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个将多个工作簿合并到一个工作VBA代码: ``` Sub MergeWorkbooks() Dim MyPath As String, FilesInPath As String Dim MyFiles() As String Dim SourceRcount As Long, FNum As Long Dim mybook As Workbook, BaseWks As Worksheet Dim sourceRange As Range, destrange As Range Dim rnum As Long, CalcMode As Long ' Change this to the path\folder location of your files. MyPath = "C:\MyDocuments\" ' Add a slash at the end of the path if needed. If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\" End If ' Set the file filter to find Excel files. FilesInPath = "*.xlsx*" ' Set the first result file number to 0. FNum = 0 ' Loop through all files in the folder. If Dir(MyPath & FilesInPath) = "" Then MsgBox "No files found." Exit Sub End If ' Turn off calculation and screen updating. With Application CalcMode = .Calculation .Calculation = xlCalculationManual .ScreenUpdating = False End With ' Set the base worksheet for the merge. Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1) ' Loop through all files. Do While Dir(MyPath & FilesInPath) <> "" ' Add to the file count. FNum = FNum + 1 ' Re-dimension the array to hold the new file name. ReDim Preserve MyFiles(1 To FNum) ' Store the file name. MyFiles(FNum) = Dir(MyPath & FilesInPath) ' Go to the next file name. DirCount = DirCount + 1 Dir Loop ' Set the starting row for the copy. rnum = 1 ' Loop through all files and worksheets, copying the data to the base worksheet. For FNum = 1 To UBound(MyFiles) Set mybook = Workbooks.Open(MyPath & MyFiles(FNum)) For Each sourceSheet In mybook.Worksheets ' Find the last row of data on the source worksheet. SourceRcount = sourceSheet.Cells(Rows.Count, "A").End(xlUp).Row ' Set the source range. Set sourceRange = sourceSheet.Range("A1:Z" & SourceRcount) ' Copy the data to the base worksheet. Set destrange = BaseWks.Range("A" & rnum) sourceRange.Copy destrange ' Increase the row counter. rnum = rnum + SourceRcount Next sourceSheet mybook.Close savechanges:=False Next FNum ' Turn on calculation and screen updating. With Application .Calculation = CalcMode .ScreenUpdating = True End With ' Auto-fit the columns on the base worksheet. BaseWks.Columns.AutoFit End Sub ``` 以下是将工作拆分多个工作簿VBA代码: ``` Sub SplitWorkbook() Dim FileExtStr As String Dim FileFormatNum As Long Dim xWs As Worksheet Dim xWb As Workbook Dim FolderName As String Dim Lrow As Long Dim OutFolder As String ' Change this to the path\folder location where you want to save the new files. OutFolder = "C:\MyDocuments\" ' Create a new folder for the output files. If Len(Dir(OutFolder, vbDirectory)) = 0 Then MkDir OutFolder End If ' Only save the active sheet. Set xWs = Application.ActiveSheet ' Get the file extension and format number. FileExtStr = ".xlsx" FileFormatNum = 51 ' Find the last row of data on the active sheet. Lrow = xWs.Cells(xWs.Rows.Count, "A").End(xlUp).Row ' Turn off calculation and screen updating. Application.ScreenUpdating = False Application.DisplayAlerts = False ' Loop through each row of data and save each row to a new file. For i = 2 To Lrow ' Create a new workbook. Set xWb = Application.Workbooks.Add ' Save the new workbook to the output folder. FolderName = OutFolder & xWs.Cells(i, "A").Value & FileExtStr ' Save the active sheet to the new workbook in the output folder. xWs.Rows(i).Copy xWb.Worksheets(1).Range("A1").PasteSpecial xlPasteAll ' Save and close the new workbook. xWb.SaveAs FolderName, FileFormatNum xWb.Close False Next i ' Turn on calculation and screen updating. Application.ScreenUpdating = True Application.DisplayAlerts = True End Sub ``` 注意,这些代码应该修改以适应您的具体情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值