网站首页 > 资源文章 正文
又找到一个小工具:
根据设置的参数,自动拆分工作表为多个工作簿文档。
主要代码,如:
Sub StartToSplit(strSavedPath As String, ByVal arrNewBookSheets As Variant)
Dim i As Integer
Dim wbNew As Workbook
Dim strwbNewName As String
Dim strwbNewFullName As String
Dim strActiveBookBaseName As String
strActiveBookBaseName = GetWorkbookBaseName()
Dim strNewSheets As String
strNewSheets = GetAllCheckedItemsName(Me.ListView1)
intMaxLen = Len(CStr(UBound(arrNewBookSheets)))
Application.ScreenUpdating = False
Dim strSplitFieldValue As String
For i = LBound(arrNewBookSheets) To UBound(arrNewBookSheets)
strSplitFieldValue = arrNewBookSheets(i)
'遇到空白单元格,直接跳出!
If Len(Trim(strSplitFieldValue)) = 0 Then
Exit Sub
End If
If Me.规则0.Value = True Then
strwbNewName = FormatNumberWithLeadingZeros(i + 1, intMaxLen) & ":" & strSplitFieldValue & ".xlsx"
End If
If Me.规则1.Value = True Then
strwbNewName = strActiveBookBaseName & "(" & strSplitFieldValue & ")" & ".xlsx"
End If
If Me.规则2.Value = True Then
If Trim(Me.TextBox自定义名称.Text) = "" Then
strwbNewName = strSplitFieldValue & ".xlsx"
Else
strwbNewName = CleanFileName(Trim(Me.TextBox自定义名称.Text)) & "(" & strSplitFieldValue & ")" & ".xlsx"
End If
End If
'创建的工作簿,有副本?
Set wbNew = CreateWorkbookWithSheets(strNewSheets)
Dim strLines As String
strLines = GetAllCheckedItemsAsString(Me.ListView1)
Call CopyLine(activeSourceWorkbook, strLines, strSplitFieldValue, wbNew)
Dim objSheetAdded As Worksheet
For Each objSheetAdded In wbNew.Worksheets
objSheetAdded.Columns.AutoFit
Next
wbNew.SaveAs (strSavedPath + strwbNewName)
Call wbNew.Close(True, strSavedPath + strwbNewName)
Set wbNew = Nothing
Next i
Application.ScreenUpdating = True
End Sub
Sub CopyLine(ByVal wbSource As Workbook, ByVal strLines As String, ByVal strSplitFieldValue As String, ByVal wbDestination As Workbook)
Dim lineArray() As String
Dim detailArray() As String
Dim strline As String
Dim strName As String
Dim intRow As Integer
Dim intColumn As Integer
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim lastRow As Long
Dim copyRange As Range
Dim cell As Range
lineArray = Split(strLines, "|")
For i = LBound(lineArray) To UBound(lineArray)
strline = lineArray(i)
detailArray = Split(strline, ";")
If UBound(detailArray) >= 2 Then
strName = detailArray(0)
intRow = CInt(detailArray(1))
intColumn = CInt(detailArray(2))
Set wsSource = wbSource.Sheets(strName)
Set wsDest = wbDestination.Sheets(strName)
Set copyRange = wsSource.Range("A1").Resize(intRow - 1, wsSource.Columns.Count)
copyRange.Copy
wsDest.Range("A1").PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
lastRow = wsDest.Cells(wsDest.Rows.Count, intColumn).End(xlUp).Row + 1
Dim hasFormula As Boolean
For Each cell In wsSource.Range(wsSource.Cells(intRow, intColumn), wsSource.Cells(wsSource.Rows.Count, intColumn))
If cell.Value = strSplitFieldValue Then
hasFormula = RowHasFormula(GetUsedCellsInRow2(cell.EntireRow))
cell.EntireRow.Copy
wsDest.Cells(lastRow, 1).PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
If hasFormula Then
wsDest.Cells(lastRow, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
wsSource.Rows(cell.Row).Copy
wsDest.Rows(lastRow).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
lastRow = lastRow + 1
End If
Next cell
Application.GoTo Reference:=wsDest.Cells(1, 1)
Application.CutCopyMode = False
End If
Next i
End Sub
Function GetColumnValues(strSheetName As String, intStartRow As Integer, intSplitColumn As Integer) As Variant
Dim ws As Worksheet
Dim lastRow As Long
Dim values() As Variant
Dim i As Long
On Error Resume Next
Set ws = activeSourceWorkbook.Worksheets(strSheetName)
If ws Is Nothing Then
GetColumnValues = Null
Exit Function
End If
lastRow = ws.Cells(ws.Rows.Count, intSplitColumn).End(xlUp).Row
If intStartRow > lastRow Then
GetColumnValues = Null
Exit Function
End If
ReDim values(1 To lastRow - intStartRow + 1)
For i = intStartRow To lastRow
values(i - intStartRow + 1) = ws.Cells(i, intSplitColumn).Value
Next i
GetColumnValues = values
End Function
Function GetFirstCheckedListViewItemValue(ByVal ListViewCtrl As ListView) As String
Dim i As Integer
For i = 1 To ListViewCtrl.ListItems.Count
If ListViewCtrl.ListItems(i).Checked Then
Dim checkedItem As ListItem
Set checkedItem = ListViewCtrl.ListItems(i)
GetFirstCheckedListViewItemValue = checkedItem.Text & ";" & checkedItem.ListSubItems(1).Text & ";" & checkedItem.ListSubItems(2).Text
Exit Function
End If
Next i
GetFirstCheckedListViewItemValue = ""
End Function
Function GetAllCheckedItemsName(ListViewCtrl As ListView) As String
Dim i As Integer
Dim result As String
result = ""
For i = 1 To ListViewCtrl.ListItems.Count
If ListViewCtrl.ListItems(i).Checked Then
result = result & ListViewCtrl.ListItems(i).Text & ";"
End If
Next i
If Len(result) > 0 Then
result = Left(result, Len(result) - 1)
End If
GetAllCheckedItemsName = result
End Function
猜你喜欢
- 2024-12-10 一个监控PLC的Android应用的开发过程-2
- 2024-12-10 android学习,listview
- 2024-12-10 ListView和RecyclerView的区别
- 2024-12-10 轻松学会:滑动组件ListView和GridView的使用
- 2024-12-10 常见面试题之ListView的复用及如何优化
- 2024-12-10 那些技术—Listview的性能提高篇
- 2024-12-10 Excel VBA,通过ListView查阅工作表数据、外部数据源数据(1/5)
- 2024-12-10 Android中ListView的使用方法
- 2024-12-10 PC SDK二次开发:基于C#语言编写的ABB机器人控制器扫描程序
- 2024-12-10 Excel vba 如何导入导出工作表,数据管理方法介绍
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- 电脑显示器花屏 (79)
- 403 forbidden (65)
- linux怎么查看系统版本 (54)
- 补码运算 (63)
- 缓存服务器 (61)
- 定时重启 (59)
- plsql developer (73)
- 对话框打开时命令无法执行 (61)
- excel数据透视表 (72)
- oracle认证 (56)
- 网页不能复制 (84)
- photoshop外挂滤镜 (58)
- 网页无法复制粘贴 (55)
- vmware workstation 7 1 3 (78)
- jdk 64位下载 (65)
- phpstudy 2013 (66)
- 卡通形象生成 (55)
- psd模板免费下载 (67)
- shift (58)
- localhost打不开 (58)
- 检测代理服务器设置 (55)
- frequency (66)
- indesign教程 (55)
- 运行命令大全 (61)
- ping exe (64)
本文暂时没有评论,来添加一个吧(●'◡'●)