方法
首先,打開要操作的excel表,如圖所示。
然后,點擊界面上方的“開發(fā)工具”,再點擊下方的“VB編輯器”,如圖所示。
然后,點擊界面上方的“插入”選項卡,再在彈出的框中點擊“模塊”,如圖所示。
然后,復(fù)制粘貼下列代碼在模塊中,如圖所示。
Sub CFGZB()
Dim myRange As Variant
Dim myArray
Dim titleRange As Range
Dim title As String
Dim columnNum As Integer
myRange = Application.InputBox(prompt:="請選擇標(biāo)題行:", 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 <> "成績單" 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) = myArray(num, 1)
Next num
.Range("A2").CopyFromRecordset conn.Execute(Sql)
End With
End With
ThisWorkbook.Activate
Sheets(1).Cells.Select
Selection.Copy
Workbooks(Nowbook.Name).Activate
ActiveSheet.Cells.Select
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”,點擊替換,查找內(nèi)容為“成績單”,替換為文件sheet表的命名。
然后,點擊界面上方的“運(yùn)行”,再點擊“運(yùn)行子過程/用戶窗體”。
然后,在彈出的窗口里選擇,條件所在位置,再點擊“確定”,如圖所示。
然后,在彈出的對話框中,點選篩選條件,如圖所示,再點擊“確定”,如圖所示。
這樣,就將一個工作表根據(jù)條件拆分成多個工作表了。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:0731-84117792 E-MAIL:11247931@qq.com