是否可以将宏粘贴到现有宏中?

时间:2016-01-07 12:38:32

标签: excel excel-vba vba

我正在尝试为excel中的员工自动化流程。我已经编写了几个宏来根据用户输入更新几个东西。

是否可以在单元格A28:H28中复制新创建的信息并将其粘贴到预先存在的宏中的特定行中?

我创建了以下复制信息的内容,然后打开相关的宏,但不知道是否可以将信息粘贴到现有的宏(称为“月”)中。

Sheets("New Month Or Client").Select
Range("A28:H28").Select
Selection.Cut
Application.CutCopyMode = False
Application.Goto Reference:="Month"

月份宏的代码是:

Sub Month()

    ' NewMonth Macro
    ' AF Report

    Sheets("AF Report").Select

        ' Copy & Paste main data

        Range("AB8:IV41").Select
        Selection.Copy

        Range("AC8").Select
        Selection.PasteSpecial Paste:=xlValues

        ' Copy & Paste Top Ten data
        Range("AA49:IW59").Select
        Selection.Copy

        Range("AG49").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("B32:B41").Select
        Selection.Copy

        Range("AA50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("F32:F41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AB50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("D32:D41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AC50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("H32:I41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AD50").Select
        Selection.PasteSpecial Paste:=xlValues

    ' MD Report

    Sheets("MD Report").Select

        ' Copy & Paste main data
        Range("AB8:IV41").Select
        Selection.Copy

        Range("AC8").Select
        Selection.PasteSpecial Paste:=xlValues

        ' Copy & Paste Top Ten data

        Range("AA49:IW59").Select
        Selection.Copy

        Range("AG49").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("B32:B41").Select
        Selection.Copy

        Range("AA50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("F32:F41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AB50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("D32:D41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AC50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("H32:I41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AD50").Select
        Selection.PasteSpecial Paste:=xlValues

    ' KK Report

    Sheets("KK Report").Select

        ' Copy & Paste main data

        Range("AB8:IV41").Select
        Selection.Copy

        Range("AC8").Select
        Selection.PasteSpecial Paste:=xlValues

        ' Copy & Paste Top Ten data

        Range("AA49:IW59").Select
        Selection.Copy

        Range("AG49").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("B32:B41").Select
        Selection.Copy

        Range("AA50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("F32:F41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AB50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("D32:D41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AC50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("H32:I41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AD50").Select
        Selection.PasteSpecial Paste:=xlValues

    ' AO Report

    Sheets("AO Report").Select

        ' Copy & Paste main data

        Range("AB8:IV41").Select
        Selection.Copy

        Range("AC8").Select
        Selection.PasteSpecial Paste:=xlValues

        ' Copy & Paste Top Ten data

        Range("AA49:IW59").Select
        Selection.Copy

        Range("AG49").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("B32:B41").Select
        Selection.Copy

        Range("AA50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("F32:F41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AB50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("D32:D41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AC50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("H32:I41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AD50").Select
        Selection.PasteSpecial Paste:=xlValues

    ' TM Report

    Sheets("TM Report").Select

        ' Copy & Paste main data

        Range("AB8:IV41").Select
        Selection.Copy

        Range("AC8").Select
        Selection.PasteSpecial Paste:=xlValues

        ' Copy & Paste Top Ten data

        Range("AA49:IW59").Select
        Selection.Copy

        Range("AG49").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("B32:B41").Select
        Selection.Copy

        Range("AA50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("F32:F41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AB50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("D32:D41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AC50").Select
        Selection.PasteSpecial Paste:=xlValues

        Range("H32:I41").Select
        Application.CutCopyMode = False
        Selection.Copy

        Range("AD50").Select
        Selection.PasteSpecial Paste:=xlValues

    ' Copy Last Month Pasted Data

    Sheets("Pasted Report").Select

        Columns("A:AR").Select
        Application.CutCopyMode = False
        Selection.Copy

        Columns("AZ:AZ").Select
        ActiveSheet.Paste

        Columns("A:AR").Select
        Selection.ClearContents

        ' Clear Aff No's & Device

        Columns("CT:CV").Select
        Selection.ClearContents

        Columns("CX:DB").Select
        Selection.ClearContents

End Sub

1 个答案:

答案 0 :(得分:2)

好的,现在我可以看到您的代码(已将其添加到您的问题中,但等待同行评审)。

您将一系列单元格传递给过程并复制/粘贴值 - 无需每次都选择工作表或范围;只是引用它们。

看看OFFSET和RESIZE,因为我希望你可以传递一个范围并从那里计算其他范围。

我是这样做的:

Sub Test()

    Dim wrkSht As Worksheet

    'ThisWorkbook is the file containing the VBA code.
    'Can also use ActiveWorkbook, Workbooks("Name.xlsx"), etc.
    For Each wrkSht In ThisWorkbook.Worksheets
        Select Case wrkSht.Name
            Case "Sheet1", "Sheet2", "AnotherSheet"
                'Do nothing.
            Case Else
                With wrkSht
                    Month .Range("AB8:IV41"), .Range("AC8"), _
                          .Range("AA49:IW59"), .Range("AG49")
                End With
        End Select
    Next wrkSht

End Sub

Sub Month(Range1 As Range, Range2 As Range, Range3 As Range, Range4 As Range)

    Range1.Copy
    Range2.PasteSpecial Paste:=xlValues

    Range3.Copy
    Range4.PasteSpecial Paste:=xlValues

End Sub