excel將一個工作表根據條件拆分成多個sheet工作表與合並多個sheet工作表


本例介紹在excel中如何將一個工作表根據條件拆分成多個工作表。 注意:很多朋友反映sheets(i).delete這句代碼出錯,要注意下面第一個步驟,要拆分的數據工作表名稱為“數據源”, 而不是你新建工作簿時的sheet1這種。手動改成“數據源”即可。或者是把代碼中得"數據源"改為你得源工作表“Sheet1”也行

Sub CFGZB()

    Dim myRange As Variant

    Dim myArray

    Dim titleRange As Range

    Dim title As String

    Dim columnNum As Integer

    myRange = Application.InputBox(prompt:="請選擇標題行:", Type:=8)

    myArray = WorksheetFunction.Transpose(myRange)

    Set titleRange = Application.InputBox(prompt:="請選擇拆分的表頭,必須是第一行,且為一個單元格,如:“姓名”", Type:=8)

    title = titleRange.Value

    columnNum = titleRange.Column

    Application.ScreenUpdating = False

    Application.DisplayAlerts = False

    Dim i&, Myr&, Arr, num&

    Dim d, k

    For i = Sheets.Count To 1 Step -1

        If Sheets(i).Name <> "Sheet1" Then
             Sheets(i).Delete

        End If

    Next i

    Set d = CreateObject("Scripting.Dictionary")

    Myr = Worksheets("Sheet1").UsedRange.Rows.Count

    Arr = Worksheets("Sheet1").Range(Cells(2, columnNum), Cells(Myr, columnNum))

    For i = 1 To UBound(Arr)

        d(Arr(i, 1)) = ""

    Next

    k = d.keys

    For i = 0 To UBound(k)

        Set conn = CreateObject("adodb.connection")

        conn.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName

        Sql = "select * from [Sheet1$] where " & title & " = '" & k(i) & "'"

        Worksheets.Add after:=Sheets(Sheets.Count)

        With ActiveSheet

            .Name = k(i)

            For num = 1 To UBound(myArray)

                .Cells(1, num) = myArray(num, 1)

            Next num

            .Range("A2").CopyFromRecordset conn.Execute(Sql)

        End With

        Sheets(1).Select

        Sheets(1).Cells.Select

        Selection.Copy

        Worksheets(Sheets.Count).Activate

        ActiveSheet.Cells.Select

        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
                               SkipBlanks:=False, Transpose:=False

        Application.CutCopyMode = False

    Next i

    conn.Close

    Set conn = Nothing

    Application.DisplayAlerts = True

    Application.ScreenUpdating = True

End Sub

 

1.將要合並的文件放在同一文件夾下,復制過來就好(ps:最好不要直接操作原數據文件,避免操作失敗,數據丟失)

2.在這個目錄下創建一個“合並.xlsx”

3.雙擊打開“合並.xlsx”

4.同時按 ALT + F11

Option Explicit

Sub mergeonexls() '合並多工作簿中指定工作表

On Error Resume Next

Dim x As Variant, x1 As Variant, w As Workbook, wsh As Worksheet

Dim t As Workbook, ts As Worksheet, l As Integer, h As Long

Application.ScreenUpdating = False

Application.DisplayAlerts = False

x = Application.GetOpenFilename(FileFilter:="Excel文件 (*.xls; *.xlsx),*.xls; *.xlsx,所有文件(*.*),*.*", Title:="Excel選擇", MultiSelect:=True)

Set t = ThisWorkbook

Set ts = t.Sheets(1) '指定合並到的工作表,這里是第一張工作表

l = ts.UsedRange.SpecialCells(xlCellTypeLastCell).Column

For Each x1 In x

If x1 <> False Then

 Set w = Workbooks.Open(x1)

 Set wsh = w.Sheets(1) '指定所需合並工作表,這里是第一張工作表

 h = ts.UsedRange.SpecialCells(xlCellTypeLastCell).Row

 If l = 1 And h = 1 And ts.Cells(1, 1) = "" Then

 wsh.UsedRange.Copy ts.Cells(1, 1)

 Else

 wsh.UsedRange.Copy ts.Cells(h + 1, 1)

 End If

 w.Close

End If

Next

Application.ScreenUpdating = True

Application.DisplayAlerts = True

End Sub

Sub mergeeveryonexls() '將多個工作簿下的工作表依次對應合並到本工作簿下的工作表,即第一張工作表對應合並到第一張,第二張對應合並到第二張……

On Error Resume Next

Dim x As Variant, x1 As Variant, w As Workbook, wsh As Worksheet

Dim t As Workbook, ts As Worksheet, i As Integer, l As Integer, h As Long

Application.ScreenUpdating = False

Application.DisplayAlerts = False

x = Application.GetOpenFilename(FileFilter:="Excel文件 (*.xls; *.xlsx),*.xls; *.xlsx,所有文件(*.*),*.*", Title:="Excel選擇", MultiSelect:=True)

Set t = ThisWorkbook

For Each x1 In x

If x1 <> False Then

 Set w = Workbooks.Open(x1)

 For i = 1 To w.Sheets.Count

If i > t.Sheets.Count Then t.Sheets.Add After:=t.Sheets(t.Sheets.Count)

 Set ts = t.Sheets(i)

 Set wsh = w.Sheets(i)

 l = ts.UsedRange.SpecialCells(xlCellTypeLastCell).Column

 h = ts.UsedRange.SpecialCells(xlCellTypeLastCell).Row

 If l = 1 And h = 1 And ts.Cells(1, 1) = "" Then

 wsh.UsedRange.Copy ts.Cells(1, 1)

 Else

 wsh.UsedRange.Copy ts.Cells(h + 1, 1)

 End If

 Next

 w.Close

End If

Next

Application.ScreenUpdating = True

Application.DisplayAlerts = True

End Sub

  

 來源:https://blog.csdn.net/qq_38545713/article/details/82500483


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM