简体   繁体   English

在生成新工作簿的同时循环浏览单个工作簿中的工作表

[英]Looping through worksheets in a single workbook while generating new workbooks

I need to loop through 47 different worksheets in a single workbook. 我需要在一个工作簿中循环浏览47个不同的工作表。

The macro being run on each worksheet is creating a new individual workbook for that worksheet. 在每个工作表上运行的宏正在为该工作表创建一个新的单独的工作簿。 My challenge is that once the worksheet from the original workbook is converted into a new workbook my macro now starts looping through the worksheet on the new workbook instead of the original. 我的挑战是,一旦将原始工作簿中的工作表转换为新工作簿,我的宏现在就开始遍历新工作簿(而不是原始工作簿)上的工作表。 I have been trying to figure out some sort of code that counts each worksheet on the original workbook and then loops back to the original once the new workbook is created. 我一直在尝试找出某种代码,该代码对原始工作簿上的每个工作表进行计数,然后在创建新工作簿后循环回到原始工作表。

Sub PriceListWest()
'
' PriceListWest Macro
'

Dim Current As Worksheet

Windows("West price list master.xlsm").Activate 'Original workbook'

For Each Current In Worksheets

    ActiveSheet.Select 'Selecting worksheet in original workbook'
    ActiveSheet.Copy 'Copying worksheet in original workbook'

    'Challenge lies here now the loop goes through the new workbook versus returning to original workbook'

    Cells.Select 
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
      :=False, Transpose:=False
    Application.CutCopyMode = False
    Selection.EntireColumn.Hidden = False

Next

End Sub

It is a bad habit to use Select and Activate unless you keep track of exactly which sheet / cell is active from time to time. 除非您不时精确地跟踪哪个工作表/单元处于活动状态,否则使用“ SelectActivate是一个坏习惯。 Refer to How to avoid using Select for tips on ways to avoid those commands. 有关避免这些命令的方法的提示,请参阅如何避免使用选择

Assuming your existing code successfully processed the first worksheet, when it started to process the second worksheet the ActiveSheet was still the recently created copy of the first worksheet. 假设您现有的代码成功处理了第一个工作表,则当它开始处理第二个工作表时, ActiveSheet仍然是第一个工作表的最近创建的副本。 So that sheet would then be copied to another workbook, rather than copying one of the worksheets from your original workbook. 这样该工作表将被复制到另一个工作簿,而不是从原始工作簿中复制一个工作表。

The following code uses a reference to the original workbook, and a reference to the Current object which your code already creates, to ensure that the correct sheets are being referenced: 以下代码使用对原始工作簿的引用以及对您的代码已创建的Current对象的引用,以确保引用了正确的图纸:

Sub PriceListWest()
'
' PriceListWest Macro
'
    Dim wbMaster As Workbook
    Dim Current As Worksheet

    'Set a reference to the original workbook
    Set wbMaster = Workbooks("West price list master.xlsm")
    'Loop through each sheet in original workbook
    For Each Current In wbMaster.Worksheets
        'Copy the "current" sheet to a new workbook
        Current.Copy
        'The "Copy" command has now made the new workbook active, and
        ' "ActiveSheet" is the newly created sheet in that workbook

        With ActiveSheet
            'Copy entire sheet
            .Cells.Copy
            'Paste values
            .Cells.PasteSpecial Paste:=xlPasteValues, _
                                Operation:=xlNone, _
                                SkipBlanks:=False, _
                                Transpose:=False
            'Unhide all columns
            .Columns.Hidden = False
            'Set "active" cell to A1, just so the whole sheet isn't selected
            .Cells(1, 1).Select
        End With
    Next
    Application.CutCopyMode = False
End Sub

The code does continue to use ActiveSheet but only in a spot where we know that the active sheet is the newly copied sheet in the newly created workbook. 该代码确实继续使用ActiveSheet但仅在我们知道活动工作表是新创建的工作簿中新复制的工作表的地方使用。

your main error lies on the fact that your Current variable holds the current worksheet while iterating through Worksheets collection, while ActiveSheet is the currently "Active" sheet and doesn't change until you Activate a new sheet (and looping doesn't Activate ) 您的主要错误在于,您的Current变量在遍历Worksheets集合时会保留当前 worksheet Worksheets ,而ActiveSheet是当前的“活动”工作表,并且在您Activate新工作表之前不会更改(并且循环不会Activate

and after every ActiveSheet.Copy , the newly created workbook becomes the ActiveWorkbook and its only sheet the ActiveSheet 在每个ActiveSheet.Copy ,新创建的工作簿将成为ActiveWorkbook ,其唯一的工作表为ActiveSheet

so you have to use Current instead of ActiveSheet 因此您必须使用Current而不是ActiveSheet

furthermore acting on all the Cells of a worksheet is time consuming and may rise memory issues: much better you refer to UsedRange property of Worksheet object 此外,对工作表的所有Cells进行操作非常耗时,并且可能会引起内存问题:更好地参考Worksheet对象的UsedRange属性

so you could code: 所以你可以编写代码:

Option Explicit

Sub PriceListWest()
    '
    ' PriceListWest Macro
    '
    Dim Current As Worksheet

    For Each Current In Workbooks("West price list master").Worksheets
        Current.Copy '<--| copy current worksheet from 'original' workbook into a new workbook, this latter becomes the "active" workbook and it's only sheet the "Active" Sheet
        With ActiveSheet.UsedRange '<--| reference "Active" worksheet of current "Active" Workbook
            .value = .value
            .EntireColumn.Hidden = False
        End With
    Next
End Sub

Finally, acting like above will leave you with as many open workbooks as pasted sheets, while you may want to save and close them at every iteration, thus leaving you with the 'original' workbook only: 最后,像上述操作将使您拥有与粘贴表一样多的打开的工作簿,而您可能希望在每次迭代时都保存并关闭它们,从而只剩下“原始”工作簿:

Option Explicit

Sub PriceListWest()
    '
    ' PriceListWest Macro
    '
    Dim Current As Worksheet

    For Each Current In Workbooks("West price list master").Worksheets
        Current.Copy '<--| copy current worksheet from 'original' workbook into a new workbook, this latter becomes the "active" workbook and it's only sheet the "Active" Sheet
        With ActiveSheet.UsedRange '<--| reference "Active" worksheet of current "Active" Workbook
            .value = .value
            .EntireColumn.Hidden = False
        End With
        ActiveWorkbook.SaveAs filepathandname '<-- save current workbook
        ActiveWorkbook.Close '<--| close it
    Next
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM