Excel 根据列值将启用宏的文件拆分为单独的文件

Excel 根据列值将启用宏的文件拆分为单独的文件,excel,vba,input,output,Excel,Vba,Input,Output,假设我有一个文件,其中已经有应用于数据的宏。我想根据区域列将该文件拆分为多个文件,这样我就必须保留该拆分文件中的所有宏函数,而不是原始文件。请告诉我用VBA做这件事的方法 Sub SplitEachWorksheet() Dim FPath As String FPath = Application.ActiveWorkbook.Path Application.ScreenUpdating = False Application.DisplayAlerts =

假设我有一个文件,其中已经有应用于数据的宏。我想根据区域列将该文件拆分为多个文件,这样我就必须保留该拆分文件中的所有宏函数,而不是原始文件。请告诉我用VBA做这件事的方法

Sub SplitEachWorksheet()
    Dim FPath As String
    FPath = Application.ActiveWorkbook.Path
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    For Each ws In ThisWorkbook.Sheets
        ws.Copy
        Application.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, 
    Filename:=FPath & "\" & ws.Name & ".xlsx"
    Application.ActiveWorkbook.Close False
    Next

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    End Sub

但我不知道如何通过保留原始文件中的宏功能来进行拆分。请告诉我如何执行此操作。

如果您想在VBA中执行此操作,我建议您将代码编写到:

  • 从区域列中查找所有值
  • 对于每个区域:
    • 制作原始文件(包括宏的)的完整副本
    • 删除不属于该区域的所有行

您必须将执行拆分和复制操作的宏放在单独的工作簿中。 我将假设该区域位于第一张图纸的第一列,并且所有相关数据都位于第一张图纸上。您必须更改代码中的位置,以匹配工作簿中的位置 我假设原始工作簿没有打开。您可能希望在代码中关闭它

Sub kopieer()
    Dim macro_wb As Workbook
    Dim macro_ws As Worksheet

    Dim orig_wb As Workbook
    Dim orig_ws As Worksheet
    Dim orig_range As Range
    Dim origpath As String
    Dim origname As String

    Dim region_wb As Workbook
    Dim region_ws As Worksheet
    Dim region As String
    Dim region_wb_name As String
    Dim region_row As Integer

    Application.ScreenUpdating = False

    origname = "D:\Oefen\test\Test_0.xlsm"

    ' Use this workbook to  find the regions
    Set macro_wb = ThisWorkbook
    Set macro_sheet = Sheet1
    macro_sheet.Cells.Clear

    Set orig_wb = Application.Workbooks.Open(Filename:=origname)
    origpath = orig_wb.Path
    ' Assuming the region is in first column of first Sheet
    Set orig_ws = orig_wb.Sheets(1)
    Set orig_range = orig_ws.Range([A2], [A2].End(xlDown))
    orig_range.Copy (Sheet1.[A1])
    orig_wb.Close

    ' Now we have all regions in column 1 of first sheet
    Sheet1.Range([A1], [A1].End(xlDown)).RemoveDuplicates Columns:=1, Header:=xlNo

    ' loop throught the regions
    For row = 1 To Sheet1.[A1].End(xlDown).row
        region = Sheet1.Cells(row, 1)
        ' Make a full copy of the original file (including the macro's)
        region_wb_name = origpath + "\" + region + ".xlsm"
        FileCopy origname, region_wb_name
        ' Delete all rows which don't belong to that region
        Set region_wb = Application.Workbooks.Open(region_wb_name)
        Set region_ws = region_wb.Sheets(1)
        ' We are deleting rows, so we should start at the bottom
        For region_row = region_ws.[A2].End(xlDown).row To 2 Step -1
            If region_ws.Cells(region_row, 1).Value <> region Then
                region_ws.Rows(region_row).Delete
            End If
        Next region_row
        region_wb.Save
        region_wb.Close
    Next row

    Application.ScreenUpdating = True
End Sub
Sub-kopieer()
作为工作簿的Dim宏\u wb
将宏作为工作表进行调整
Dim orig_wb作为工作簿
Dim orig_ws As工作表
变暗原点范围作为范围
Dim origpath作为字符串
朦胧的原名如弦
Dim区域\u wb作为工作簿
调暗区域作为工作表
作为字符串的暗淡区域
Dim区域\u wb\u名称为字符串
Dim region_行为整数
Application.ScreenUpdating=False
origname=“D:\Oefen\test\test\u 0.xlsm”
'使用此工作簿查找区域
设置宏\u wb=此工作簿
设置宏_sheet=Sheet1
宏_sheet.Cells.Clear
设置orig_wb=Application.Workbooks.Open(文件名:=origname)
origpath=orig_wb.Path
'假设区域位于第一张图纸的第一列中
设置原始工作表=原始工作表(1)
设置原始范围=原始范围([A2],[A2]。结束(xlDown))
原稿(第1页[A1])
原wb.Close
'现在我们在第一页的第1列中有了所有区域
Sheet1.范围([A1],[A1]。结束(xlDown))。移除的重复列:=1,标题:=xlNo
"环游各地区",
对于第1行到第1页[A1]。结束(xlDown)。行
区域=表1。单元格(第1行)
'制作原始文件(包括宏的)的完整副本
region\u wb\u name=origpath+“\”+region+“.xlsm”
文件副本原始名称、区域\u wb\u名称
'删除不属于该区域的所有行
设置region\u wb=Application.Workbooks.Open(region\u wb\u name)
设置区域工作表=区域工作表(1)
'我们正在删除行,因此应该从底部开始
对于区域\行=区域\ ws.[A2]。结束(xlDown)。行到2步骤-1
如果区域为单元格(区域为行,1)。则值为区域
区域行(区域行)。删除
如果结束
下一区域_行
区域\ wb.Save
地区_wb.Close
下一排
Application.ScreenUpdating=True
端接头

如果您想在VBA中执行此操作,我建议您将代码编写到:

  • 从区域列中查找所有值
  • 对于每个区域:
    • 制作原始文件(包括宏的)的完整副本
    • 删除不属于该区域的所有行

您必须将执行拆分和复制操作的宏放在单独的工作簿中。 我将假设该区域位于第一张图纸的第一列,并且所有相关数据都位于第一张图纸上。您必须更改代码中的位置,以匹配工作簿中的位置 我假设原始工作簿没有打开。您可能希望在代码中关闭它

Sub kopieer()
    Dim macro_wb As Workbook
    Dim macro_ws As Worksheet

    Dim orig_wb As Workbook
    Dim orig_ws As Worksheet
    Dim orig_range As Range
    Dim origpath As String
    Dim origname As String

    Dim region_wb As Workbook
    Dim region_ws As Worksheet
    Dim region As String
    Dim region_wb_name As String
    Dim region_row As Integer

    Application.ScreenUpdating = False

    origname = "D:\Oefen\test\Test_0.xlsm"

    ' Use this workbook to  find the regions
    Set macro_wb = ThisWorkbook
    Set macro_sheet = Sheet1
    macro_sheet.Cells.Clear

    Set orig_wb = Application.Workbooks.Open(Filename:=origname)
    origpath = orig_wb.Path
    ' Assuming the region is in first column of first Sheet
    Set orig_ws = orig_wb.Sheets(1)
    Set orig_range = orig_ws.Range([A2], [A2].End(xlDown))
    orig_range.Copy (Sheet1.[A1])
    orig_wb.Close

    ' Now we have all regions in column 1 of first sheet
    Sheet1.Range([A1], [A1].End(xlDown)).RemoveDuplicates Columns:=1, Header:=xlNo

    ' loop throught the regions
    For row = 1 To Sheet1.[A1].End(xlDown).row
        region = Sheet1.Cells(row, 1)
        ' Make a full copy of the original file (including the macro's)
        region_wb_name = origpath + "\" + region + ".xlsm"
        FileCopy origname, region_wb_name
        ' Delete all rows which don't belong to that region
        Set region_wb = Application.Workbooks.Open(region_wb_name)
        Set region_ws = region_wb.Sheets(1)
        ' We are deleting rows, so we should start at the bottom
        For region_row = region_ws.[A2].End(xlDown).row To 2 Step -1
            If region_ws.Cells(region_row, 1).Value <> region Then
                region_ws.Rows(region_row).Delete
            End If
        Next region_row
        region_wb.Save
        region_wb.Close
    Next row

    Application.ScreenUpdating = True
End Sub
Sub-kopieer()
作为工作簿的Dim宏\u wb
将宏作为工作表进行调整
Dim orig_wb作为工作簿
Dim orig_ws As工作表
变暗原点范围作为范围
Dim origpath作为字符串
朦胧的原名如弦
Dim区域\u wb作为工作簿
调暗区域作为工作表
作为字符串的暗淡区域
Dim区域\u wb\u名称为字符串
Dim region_行为整数
Application.ScreenUpdating=False
origname=“D:\Oefen\test\test\u 0.xlsm”
'使用此工作簿查找区域
设置宏\u wb=此工作簿
设置宏_sheet=Sheet1
宏_sheet.Cells.Clear
设置orig_wb=Application.Workbooks.Open(文件名:=origname)
origpath=orig_wb.Path
'假设区域位于第一张图纸的第一列中
设置原始工作表=原始工作表(1)
设置原始范围=原始范围([A2],[A2]。结束(xlDown))
原稿(第1页[A1])
原wb.Close
'现在我们在第一页的第1列中有了所有区域
Sheet1.范围([A1],[A1]。结束(xlDown))。移除的重复列:=1,标题:=xlNo
"环游各地区",
对于第1行到第1页[A1]。结束(xlDown)。行
区域=表1。单元格(第1行)
'制作原始文件(包括宏的)的完整副本
region\u wb\u name=origpath+“\”+region+“.xlsm”
文件副本原始名称、区域\u wb\u名称
'删除不属于该区域的所有行
设置region\u wb=Application.Workbooks.Open(region\u wb\u name)
设置区域工作表=区域工作表(1)
'我们正在删除行,因此应该从底部开始
对于区域\行=区域\ ws.[A2]。结束(xlDown)。行到2步骤-1
如果区域为单元格(区域为行,1)。则值为区域
区域行(区域行)。删除
如果结束
下一区域_行
区域\ wb.Save
地区_wb.Close
下一排
Application.ScreenUpdating=True
端接头

谢谢你告诉我们你想要什么。现在,在你的问题中加入你在做研究后所做的尝试。请记住,Stack Overflow不是一个免费的代码编写服务,您只需发布您想要的内容,别人就会给您代码。请不要在注释中发布您的代码。把它包括在你的问题中