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
相關文章
- mysql like查詢 - 根據多個條件的模糊匹配查詢MySql
- Excel快速合併多張Excel工作表教程 Excel工作表怎麼合併?Excel
- 根據查詢條件批量修改表資料
- Vue根據條件新增click事件Vue事件
- Pandas根據篩選條件對指定excel列進行篩選!神器!Excel
- element table的selection根據條件禁用
- 多個報表匯出到一個 excel 的多 sheet 頁Excel
- sqlserver根據條件去除重複資料SQLServer
- 一個故事看懂記憶體條工作原理記憶體
- 多個excel檔案合併成一個excel表的方法 如何快速合併多個excel檔案Excel
- C# net8使用NPOI匯出多個sheet工作簿的execl檔案C#
- Python操作excel(將多張excel表融合到一張表)PythonExcel
- Excel 這 10 個導航快捷鍵幫助你工作表裡不迷路Excel
- 快速查詢EXCEL整個工作表中的合併單元格Excel
- 多個excel檔案合併到一個檔案中的多個sheet表中Excel
- 如在 Java 中分割 Excel 工作表JavaExcel
- 根據某個查詢條件的前50條資料來決定UPDATE語句的更新範圍
- 同一欄位多個查詢條件時遇到的一個問題
- 如何透過C++ 將資料寫入 Excel 工作表C++Excel
- 4個Excel技巧,提高你的工作效率!Excel
- 50個工作中最常用excel技巧 excel常用技巧大全Excel
- Excel_不開啟檔案進行跨工作簿查詢Excel
- 一個表單同時提交多條記錄
- VBA中使用EXCEL工作表函式Excel函式
- VBS遍歷Excel工作表的方法Excel
- mybatis 根據多個id查詢資料 foreach標籤MyBatis
- sql根據多個欄位查詢重複記錄SQL
- 附個人工作程式碼 條件變數深度運用、互斥鎖+訊號量變數
- Excel 條件格式Excel
- Laravel 多條件查詢時粗心導致的一個 BUGLaravel
- jQuery DataTables新增自定義多個搜尋條件jQuery
- excel條件格式怎麼設定 excel條件格式在哪裡Excel
- Excel2007工作表如何分視窗顯示?Excel2007工作表分視窗顯示的方法Excel
- 聊聊spring專案如何根據事件條件進行事件分發Spring事件
- 3. 工作分配問題(回溯法)設有n件工作分配給n個人。。。
- 從多個方面瞭解工作流自定義表單的優勢
- 用excel表畫一個樂高Excel
- Excel教程——excel如何使用條件格式Excel