excel將一個工作表根據條件拆分成多個工作簿
Function FilePicker() As String
'新建一個對話方塊物件
'MsoFileDialogType 可為以下 MsoFileDialogType 常量之一。
'msoFileDialogFilePicker 允許使用者選擇檔案。
'msoFileDialogFolderPicker 允許使用者選擇一個資料夾
'msoFileDialogOpen 允許使用者開啟檔案
'msoFileDialogSaveAs
Set FileDialogObject = Application.FileDialog(msoFileDialogFolderPicker)
'配置對話方塊
With FileDialogObject
.title = "請選擇檔案"
.InitialFileName = "D:\"
.AllowMultiSelect = False
End With
'顯示對話方塊
FileDialogObject.Show
'獲取選擇對話方塊選擇的檔案
Set paths = FileDialogObject.SelectedItems
FilePicker = paths(1)
End Function
'拆分工作表 (選擇拆分儲存目錄)
Sub CFGZB()
Dim myRange As Variant
Dim myArray
Dim titleRange As Range
Dim title As String
Dim columnNum As Integer
Dim sheetName As String
Dim savePath As String
Dim fieldTypeName As String
sheetName = "Sheet1"
savePath = FilePicker()
If Len(savePath) = 0 Then
savePath = "D:/"
End If
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, fileName
For i = Sheets.Count To 1 Step -1
If Sheets(i).name <> sheetName Then
Sheets(i).Delete
End If
Next i
Set d = CreateObject("Scripting.Dictionary")
Myr = Worksheets(sheetName).UsedRange.Rows.Count
Arr = Worksheets(sheetName).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
fieldTypeName = TypeName(k(i))
fileName = k(i)
If fieldTypeName = "String" Then
Sql = "select * from [" & sheetName & "$] where " & title & " = '" & k(i) & "'"
ElseIf fieldTypeName = "Date" Then
Sql = "select * from [" & sheetName & "$] where " & title & " = #" & k(i) & "# "
fileName = Replace(fileName, "/", "-")
fileName = Replace(fileName, "\", "-")
Else
Sql = "select * from [" & sheetName & "$] where " & title & " = " & k(i)
End If
'MsgBox (Sql)
Dim Nowbook As Workbook
Set Nowbook = Workbooks.Add
With Nowbook
With .Sheets(1)
.name = fileName
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 savePath & "\" & fileName
Nowbook.Close True
Set Nowbook = Nothing
Next i
conn.Close
Set conn = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
''拆分工作表 (拆分目錄寫死為 D:/xls)
Sub CFGZB()
Dim myRange As Variant
Dim myArray
Dim titleRange As Range
Dim title As String
Dim columnNum As Integer
Dim sheetName As String
Dim savePath As String
Dim fieldTypeName As String
sheetName = "Sheet1"
savePath = "D:/xls"
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, fileName
For i = Sheets.Count To 1 Step -1
If Sheets(i).name <> sheetName Then
Sheets(i).Delete
End If
Next i
Set d = CreateObject("Scripting.Dictionary")
Myr = Worksheets(sheetName).UsedRange.Rows.Count
Arr = Worksheets(sheetName).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
fieldTypeName = TypeName(k(i))
fileName = k(i)
If fieldTypeName = "String" Then
Sql = "select * from [" & sheetName & "$] where " & title & " = '" & k(i) & "'"
ElseIf fieldTypeName = "Date" Then
Sql = "select * from [" & sheetName & "$] where " & title & " = #" & k(i) & "# "
fileName = Replace(fileName, "/", "-")
fileName = Replace(fileName, "\", "-")
Else
Sql = "select * from [" & sheetName & "$] where " & title & " = " & k(i)
End If
'MsgBox (Sql)
Dim Nowbook As Workbook
Set Nowbook = Workbooks.Add
With Nowbook
With .Sheets(1)
.name = fileName
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 savePath & "\" & fileName
Nowbook.Close True
Set Nowbook = Nothing
Next i
conn.Close
Set conn = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
相關文章
- 如何將Excel中多個不同的工作表彙總成一張總表Excel
- mysql like查詢 - 根據多個條件的模糊匹配查詢MySql
- 使用SQLLOADER將每行資料根據條件插入不同表SQL
- Excel快速合併多張Excel工作表教程 Excel工作表怎麼合併?Excel
- 根據查詢條件批量修改表資料
- 根據條件動態更新不同表的資料
- 合併多個工作薄workbooks到一個工作薄workbook
- Vue根據條件新增click事件Vue事件
- Pandas根據篩選條件對指定excel列進行篩選!神器!Excel
- jQuery根據多個屬性匹配元素jQuery
- 多個報表匯出到一個 excel 的多 sheet 頁Excel
- 多個excel檔案合併成一個excel表的方法 如何快速合併多個excel檔案Excel
- 一個故事看懂記憶體條工作原理記憶體
- [Q]怎麼實現一條記錄根據條件多表插入 zt
- goldegate根據實際要求進行對映到多個不同表Go
- Excel不同列多條件計數Excel
- 多個excel檔案合併到一個檔案中的多個sheet表中Excel
- 一個根據已有表結構來建立新表的儲存過程儲存過程
- 同一欄位多個查詢條件時遇到的一個問題
- 根據某個查詢條件的前50條資料來決定UPDATE語句的更新範圍
- EXCEL 2010 使用共享工作簿進行協作Excel
- Excel 這 10 個導航快捷鍵幫助你工作表裡不迷路Excel
- 快速查詢EXCEL整個工作表中的合併單元格Excel
- Python操作excel(將多張excel表融合到一張表)PythonExcel
- 如在 Java 中分割 Excel 工作表JavaExcel
- 根據一個筆試題引出的思考筆試
- 如何透過C++ 將資料寫入 Excel 工作表C++Excel
- 4個Excel技巧,提高你的工作效率!Excel
- 50個工作中最常用excel技巧 excel常用技巧大全Excel
- excel根據某一列匹配資料Excel
- Excel_不開啟檔案進行跨工作簿查詢Excel
- sql根據多個欄位查詢重複記錄SQL
- jxls根據模板匯出excelExcel
- apose 根據excel 匯出模版Excel
- VBS遍歷Excel工作表的方法Excel
- VBA中使用EXCEL工作表函式Excel函式
- c# 獲取excel所有工作表C#Excel
- EXCEL工作表保護密碼破解Excel密碼