問題已解決
老師,一個總 表怎么按業(yè)務(wù)員分成幾個分表
溫馨提示:如果以上題目與您遇到的情況不符,可直接提問,隨時(shí)問隨時(shí)答
速問速答點(diǎn)擊【開發(fā)工具】-【Visual Basic】或者Alt+F11的快捷鍵進(jìn)入VBE編輯界面。
插入一個新的模塊
粘貼下列代碼在模塊中:
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 <> 數(shù)據(jù)源 Then
Sheets(i).Delete
End If
Next i
Set d = CreateObject(Scripting.Dictionary)
Myr = Worksheets(數(shù)據(jù)源).UsedRange.Rows.Count
Arr = Worksheets(數(shù)據(jù)源).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 [數(shù)據(jù)源$] 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
插入一個控件按鈕,并指定宏到剛才插入的模塊代碼。
點(diǎn)擊插入的按鈕控件,根據(jù)提示選擇標(biāo)題行和要拆分的列字段
2019 12/04 21:37
閱讀 1168