vba - 在 Excel 中将文件拆分为多个文件

标签 vba xslt excel

我在 Excel 中有以下文件:

NAME VALUE
ABC 10
ABC 11
ABC 12
DEF 20
DEF 21
DEF 22
GHI 30
GHI 31
GHI 32

我想按“名称”列将其拆分为文件(以上示例为 3 个文件),如下所示:

文件: ABC.xsl
NAME VALUE
ABC 10
ABC 11
ABC 12

文件: DEF.xsl
NAME VALUE
DEF 20
DEF 21
DEF 22

文件: GHI.xsl
NAME VALUE
GHI 30
GHI 31
GHI 32

到目前为止,尝试了以下宏:
https://sites.google.com/a/madrocketscientist.com/jerrybeaucaires-excelassistant/parse-functions/sheet1-to-wbs

此行出现运行时错误 ws.Range(vTitles).AutoFilter在将其注释掉后,错误移至 ws.Range(vTitles).AutoFilter Field:=vCol, Criteria1:=MyArr(Itm)vCol的值变为空。

请问我做错了什么? (因为 VBA 不是我的强项)。任何关于上述代码段或可行的替代代码的建议对我来说都是一个可行的解决方案。

最佳答案

我认为这应该能让你到达你要去的地方。下面的代码将每个组作为工作簿(.xls 格式)保存在与包含 VBA 的工作簿相同的目录中(即 ThisWorkbook ):

Option Explicit
Sub SplitIntoSeperateFiles()

Dim OutBook As Workbook
Dim DataSheet As Worksheet, OutSheet As Worksheet
Dim FilterRange As Range
Dim UniqueNames As New Collection
Dim LastRow As Long, LastCol As Long, _
    NameCol As Long, Index As Long
Dim OutName As String

'set references and variables up-front for ease-of-use
Set DataSheet = ThisWorkbook.Worksheets("Sheet1")
NameCol = 1
LastRow = DataSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastCol = DataSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Set FilterRange = Range(DataSheet.Cells(1, NameCol), DataSheet.Cells(LastRow, LastCol))

'loop through the name column and store unique names in a collection
For Index = 2 To LastRow
    On Error Resume Next
        UniqueNames.Add Item:=DataSheet.Cells(Index, NameCol), Key:=DataSheet.Cells(Index, NameCol)
    On Error GoTo 0
Next Index

'iterate through the unique names collection, writing 
'to new workbooks and saving as the group name .xls
Application.DisplayAlerts = False
For Index = 1 To UniqueNames.Count
    Set OutBook = Workbooks.Add
    Set OutSheet = OutBook.Sheets(1)
    With FilterRange
        .AutoFilter Field:=NameCol, Criteria1:=UniqueNames(Index)
        .SpecialCells(xlCellTypeVisible).Copy OutSheet.Range("A1")
    End With
    OutName = ThisWorkbook.FullName
    OutName = Left(OutName, InStrRev(OutName, "\"))
    OutName = OutName & UniqueNames(Index)
    OutBook.SaveAs Filename:=OutName, FileFormat:=xlExcel8
    OutBook.Close SaveChanges:=False
    Call ClearAllFilters(DataSheet)
Next Index
Application.DisplayAlerts = True

End Sub

'safely clear all the filters on data sheet
Sub ClearAllFilters(TargetSheet As Worksheet)
    With TargetSheet
        TargetSheet.AutoFilterMode = False
        If .FilterMode Then
            .ShowAllData
        End If
    End With
End Sub

关于vba - 在 Excel 中将文件拆分为多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23106555/

相关文章:

vba - 根据列和行总计计算值矩阵

excel - 搜索lastrow VBA Excel时对象_worksheet的方法 "Range"失败

php - XML "tree"到多级 html 列表

xslt - XSL 输出方法文本,包括 xsl 中的空格

excel - VBA 格式化函数被忽略

excel - 在 Excel 2010 中生成数据透视表的 VBA 代码

xslt - 具有动态匹配的xslt调用模板

excel - 计算具有相同背景颜色的单元格列表

用于在图表上设置标签的 Vba 代码

excel - 运行时错误 '9' vba 代码;在不知道名称的情况下打开 xlsx 文件并对其进行更改