将一张纸复印到具有动态范围的多张纸的宏

时间:2015-06-26 13:08:34

标签: excel vba

VBA奇才,

使用以下代码,我能够生成多个工作表名称等于动态范围的工作表:

Sub SheetMacro()
    Dim MyCell As Range, MyRange As Range

    Set MyRange = Sheets("Control_Sheet").Range("F7")
    Set MyRange = Range(MyRange, MyRange.End(xlDown))

    For Each MyCell In MyRange
        Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
        Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
    Next MyCell
End Sub

尝试将带有公式(EAC摘要)的源模板复制到所有最近创建的工作表中时,我遇到了一些麻烦。添加此代码会复制EAC摘要,但不会在动态范围内命名选项卡:

Sub SheetMacro()
    Dim MyCell As Range, MyRange As Range
    Dim ws1 As Worksheet

    Set MyRange = Sheets("Control_Sheet").Range("F7")
    Set MyRange = Range(MyRange, MyRange.End(xlDown))

    For Each MyCell In MyRange
        Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
        Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet

        Set ws1 = ThisWorkbook.Worksheets("EAC Summary")
        ws1.Copy ThisWorkbook.Sheets(Sheets.Count)

    Next MyCell  

End Sub

是否有一种最佳方法可以通过这种方式组合代码来复制源模板,并根据动态范围内的输入命名选项卡?

提前致谢。

1 个答案:

答案 0 :(得分:0)

我能够利用替代命令进行复制,这解决了我最初的问题。这是代码,对于任何可能遇到这种情况的人来说:

Sub SheetMacro3()
Dim MyCell As Range, MyRange As Range

Set MyRange = Sheets("Control_Sheet").Range("F7")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
Sheets("EAC Summary").Visible = True   'displays master template
For Each MyCell In MyRange

    Sheets("EAC Summary").Copy After:=Worksheets(Worksheets.Count)
    Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
Next MyCell
Sheets("EAC Summary").Visible = False 'hides master template
End Sub