在excel中,经常会用到将一个工作表根据条件拆分成多个工作表,那你是否知道如何操作呢?下面就心米的业张奏察数工苏初来分享一下其操作步骤。
方法
首先,打开要操作的excel表,如图所示。
然后,点击界面上方的“开发工具”,再点击下方的“VB编辑器”,如图所示。
然后,点击界面上方的“插入”选项卡,再在弹出的框中点击“模块”,如图所示。
然后,复制粘贴下列代析码在模块中,如图所示。
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 Shee之即土密第班ts(i).Name <> "成绩单" Then
Sheets(i).Delete
End If
Next i
Set d = CreateObject("Scripting.Dictionary")
Myr = Worksheets("成绩单").UsedRange.Rows.Count
Arr = Worksheets("成绩单").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 [成绩单$] where " & title & " = '" & k(i) & "'"
Dim Nowbook As Workbook
Set Nowbook = Workbooks.Add
With Nowbook
With .Sheets(1)
.Name = k(i)
For num = 1 To UBound(myArray)
.Cells(1, num) = myArra斤y(num, 1)
Next num
.Range("A2").CopyFromRecordset conn.Execute(Sql)
End With
End With
ThisWorkbook.Activa令错整te
Sheets(1).Cells.Select
Selection.Copy
Workbooks(Nowbook.Name).Activate
ActiveSheet.Cells.Selec田化灯队右t
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Nowbook.SaveAs ThisWorkbook.Path & "\" & k(i)
Nowbook.Close True
Set Nowbook = Nothing
Next i
conn.Close
Set conn = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
然后,按组合键“CTRAL+F”,点击替换,查找内容为“成绩单”,替换为文件sheet表的命名。
然后,点击界面上方的“运行”,再点击“运行子过程/用户窗体”。
然后,在弹出的窗口里选择,条件所在位置,再点击“确定”,如图所示。
然后,在弹出的对话框中,点选筛选条件,如图所示,再点击“确定”,如图所示。
这样,就将一个工作表根据条件拆分成多个工作表了。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 951076433@qq.com 举报,一经查实,本站将立刻删除。如若转载,请注明出处:https://www.xiaomafuwu.com/228073.html