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