office交流網(wǎng)--QQ交流群號及微信交流群

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

微信交流群(請用微信掃碼)

        

Excel拆分工作表到多個工作簿中,同時添加數(shù)據(jù)到表中

2020-01-18 08:00:00
轉貼
4211

在Excel中,我們需要把一個工作表的數(shù)據(jù)拆分到多個獨立的工作表中。

即以數(shù)據(jù)的某個字段命名創(chuàng)建工作簿,然后在對應的工作表中的添加該對應的數(shù)據(jù)


運行下面代碼,把5條數(shù)據(jù)拆分到5個新建的表中。

Sub 拆分工作表()
    Dim arr, d As Object, k, t, i&, lc%, rng As Range, c%
    c = Application.InputBox("請輸入拆分列號", , 4, , , , , 1)
    If c = 0 Then Exit Sub
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    arr = [a1].CurrentRegion
    lc = UBound(arr, 2)
    Set rng = [a1].Resize(, lc)
    Set d = CreateObject("scripting.dictionary")
    For i = 2 To UBound(arr)
        If Not d.Exists(arr(i, c)) Then
            Set d(arr(i, c)) = Cells(i, 1).Resize(1, lc)
        Else
            Set d(arr(i, c)) = Union(d(arr(i, c)), Cells(i, 1).Resize(1, lc))
        End If
    Next
    k = d.Keys
    t = d.Items
    For i = 0 To d.Count - 1
        With Workbooks.Add(xlWBATWorksheet)
            rng.Copy .Sheets(1).[a1]
            t(i).Copy .Sheets(1).[a2]
            .SaveAs Filename:="F:\新建文件夾" & k(i) & ".xls"
            .Close
        End With
    Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox "完畢"
End Sub

創(chuàng)建文件成功





在創(chuàng)建的“人事部”工作簿中追加數(shù)據(jù)“人事部”的數(shù)據(jù)

    分享
    聯(lián)系我們
    聯(lián)系人: 王先生
    Email: 18449932@qq.com
    QQ: 18449932
    微博: officecn01
    移動訪問