热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

根据列值复制工作表-Copyworksheetsbasedoncolumnvalue

IamfairlynewwithExcelvbabuthavebeenusingaccessvbaforsometimenow.我对Excelvba相当新,但现在已

I am fairly new with Excel vba but have been using access vba for some time now.

我对Excel vba相当新,但现在已经使用访问vba一段时间了。

I have some code which splits a main file into several other files based on a distinct column in excel

我有一些代码可以根据excel中的不同列将主文件拆分成其他几个文件

Sub SplitbyValue()
   Dim FromR As Range, ToR As Range, All As Range, Header As Range
   Dim Wb As Workbook
   Dim Ws As Worksheet
  'Get the header in this sheet
   Set Header = Range("D8").EntireRow

  'Visit each used cell in column D, except the header
   Set FromR = Range("D9")
   For Each ToR In Range(FromR, Range("D" & Rows.Count).End(xlUp).Offset(1))
     'Did the value change?
     If FromR <> ToR Then
       'Yes, get the cells between
       Set All = Range(FromR, ToR.Offset(-1)).EntireRow
       'Make a new file



       Set Wb = Workbooks.Add(xlWBATWorksheet)
        'Copy the data into there


       With Wb.ActiveSheet
         Header.Copy .Range("A8")
         All.Copy .Range("A9")
       End With
       'Save it


       Wb.SaveAs ThisWorkbook.Path & "\" & Format(Date, "yyyy.mm.dd") & _
         " - " & FromR.Value & ".xls", xlWorkbookNormal
       Wb.Close
       'Remember the start of this section
       Set FromR = ToR
     End If
   Next
 End Sub

This works great for the main sheet, but have to copy multiple tabs and this only captures one sheet. How can I expand this so it copies the other sheets as well into that file?

这适用于主工作表,但必须复制多个选项卡,这只能捕获一个工作表。如何扩展它以便将其他工作表复制到该文件中?

example: ColumnA Id1 Id2 Id3

示例:ColumnA Id1 Id2 Id3

This creates three files (Id1)(Id2)(Id3) but ignores the other sheets.

这将创建三个文件(Id1)(Id2)(Id3)但忽略其他工作表。

2 个解决方案

#1


0  

Here is a function that will allow you to search for a sheet and goto it by name.

这是一个允许您搜索工作表并按名称转到它的功能。

 Private Sub loopsheets(strSheetName As String)
    iFoundWorksheet = 0
    For iIndex = 1 To ea.ActiveWorkbook.Worksheets.Count
        Set ws = ea.Worksheets(iIndex)
        If UCase(ws.Name) = UCase(strSheetName) Then
            iFoundWorksheet = iIndex
            Exit For
        End If
    Next iIndex
    If iFoundWorksheet = 0 Then
        MsgBox "No worksheet was found with the name RESULTS (this is not case sensetive). Aborting."
    End If
    Set ws = ea.Worksheets(iFoundWorksheet)
    ws.Activate

End Sub

If you want to just loop them all you just need the for loop.

如果你想只循环它们,你只需要for循环。

    Dim iIndex as Integer
    For iIndex = 1 To ea.ActiveWorkbook.Worksheets.Count
        Set ws = ea.Worksheets(iIndex)
        ws.Activate

        'Call your code here.
        SplitbyValue

    Next iIndex

#2


0  

Create an encompassing loop and define the worksheet being processed with a With...End With statement. You loop through a For Each...Next Statement using a Worksheet object on the Worksheets collection but I typically use the index of each worksheet.

创建一个包含循环并使用With ... End With语句定义正在处理的工作表。使用Worksheets集合上的Worksheet对象循环遍历For Each ... Next语句,但我通常使用每个工作表的索引。

Sub SplitbyValue()
    Dim FromR As Range, ToR As Range, dta As Range, hdr As Range
    Dim w As Long, ws As Worksheet, wb As Workbook, nuwb As Workbook

    'Get the header in this sheet

    Set wb = ActiveWorkbook

    For w = 1 To wb.Worksheets.Count
        With wb.Worksheets(w)
            Set hdr = .Range(.Cells(8, "D"), .Cells(8, Columns.Count).End(xlToLeft))

            'Visit each used cell in column D, except the header
            Set FromR = .Range("D9")
            For Each ToR In .Range(FromR, .Range("D" & Rows.Count).End(xlUp).Offset(1))
                'Did the value change?
                If FromR <> ToR Then
                    'Yes, get the cells between
                    Set dta = .Range(FromR, ToR.Offset(-1)).EntireRow

                    'Make a new file
                    Set nuwb = Workbooks.Add(xlWBATWorksheet)

                    'Copy the data into there
                    With nuwb.Sheet1
                         hdr.Copy .Range("A8")
                         dta.Copy .Range("A9")
                    End With

                    'Save it
                    nuwb.SaveAs ThisWorkbook.Path & "\" & Format(Date, "yyyy.mm.dd") & _
                        " - " & FromR.Value & ".xls", xlWorkbookNormal
                    nuwb.Close False
                    Set nuwb = Nothing

                    'Remember the start of this section
                    Set FromR = ToR
                End If
            Next ToR

        End With
    Next w
End Sub

I did not set up a full test environment but this should get you heading in the right direction. I've always found it unreliable to depend on ActiveSheet.

我没有建立一个完整的测试环境,但这应该让你朝着正确的方向前进。我一直觉得依赖ActiveSheet是不可靠的。


推荐阅读
author-avatar
励志天涯网
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有