Excel复制粘贴区域不相同

时间:2016-05-31 07:21:35

标签: excel-vba error-handling excel-formula vba excel

你好,我有这个Excel宏,通过复制粘贴现有工作表中的某些东西来生成新工作表。基本上前三个段落重复三次,最后的第10段只是填充一列。但是对于第7,第8和第9段,它显示了我  '错误 1004副本和贴纸区域不一样',而它正在上面工作而没有任何此类问题。

    Sub Macro1()
    '
    ' Macro1 Macro
    '

    '
        Sheets.Add After:=ActiveSheet
        Sheets("Sheet1").Select
        Sheets("Sheet1").Move Before:=Sheets(2)
        Range("A1").Select
        ActiveCell.FormulaR1C1 = "Product Number"
        Range("B1").Select
        ActiveCell.FormulaR1C1 = "Product Desc"
        Range("C1").Select
        ActiveCell.FormulaR1C1 = "Service Type"
        Range("D1").Select
        ActiveCell.FormulaR1C1 = "Service Level"
        Range("E1").Select
        ActiveCell.FormulaR1C1 = "Service P/N"
        Range("F1").Select
        ActiveCell.FormulaR1C1 = "APAC(USD)"
        Range("F2").Select
Columns("A:A").ColumnWidth = 14.27
Columns("B:B").ColumnWidth = 15.13
Columns("C:C").ColumnWidth = 13.27
Columns("D:D").ColumnWidth = 13.13
Columns("E:E").ColumnWidth = 14.33
Columns("F:F").ColumnWidth = 12.07

Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A2").Select
ActiveSheet.Paste
Columns("B:B").ColumnWidth = 40

Sheets("1. SMARTnet ").Select
Range("D9:E20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("E2").Select
ActiveSheet.Paste

Range("D2").Select
Sheets("1. SMARTnet ").Select
Range("D1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("D2").Select
ActiveSheet.Paste
Columns("D:D").ColumnWidth = 17.07
Range("D3").Select
ActiveSheet.Paste
Range("D2:D3").Select
Selection.AutoFill Destination:=Range("D2:D20687"), Type:=xlFillDefault
Range("D2:D20687").Select



Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A20688").Select
ActiveSheet.Paste

Sheets("1. SMARTnet ").Select
Range("F9:G20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("E20688").Select
ActiveSheet.Paste

Range("D2").Select
Sheets("1. SMARTnet ").Select
Range("F1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("D20688").Select
ActiveSheet.Paste
Range("D20689").Select
ActiveSheet.Paste
Range("D20688:D20689").Select
Selection.AutoFill Destination:=Range("D20688:D41373"), Type:=xlFillDefault
Range("D20688:D41373").Select

Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A41374").Select
ActiveSheet.Paste

Sheets("1. SMARTnet ").Select
Range("H9:I20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("E41374").Select
ActiveSheet.Paste

Range("D2").Select
Sheets("1. SMARTnet ").Select
Range("H1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("D41374").Select
ActiveSheet.Paste
Range("D41375").Select
ActiveSheet.Paste
Range("D41374:D41375").Select
Selection.AutoFill Destination:=Range("D41374:D62059"), Type:=xlFillDefault
Range("D41374:D62059").Select

Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("A62060").Select
ActiveSheet.Paste

Sheets("1. SMARTnet ").Select
Range("P9:P20694,U9:U20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("E62060").Select
ActiveSheet.Paste

Range("D2").Select
Sheets("1. SMARTnet ").Select
Range("P1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("D62060").Select
ActiveSheet.Paste
Range("D62061").Select
ActiveSheet.Paste
Range("D62060:D62061").Select
Selection.AutoFill Destination:=Range("D62060:D82745"), Type:=xlFillDefault
Range("D62060:D82745").Select



Range("C2").Select
ActiveCell.FormulaR1C1 = "SMARTnet"
Selection.AutoFill Destination:=Range("C2:C82745"), Type:=xlFillDefault
Range("C2:C82745").Select

End Sub

任何提示都将受到赞赏:)

1 个答案:

答案 0 :(得分:0)

我认为你应该通过不使用“select”

来简化你的代码

例如 而不是

Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A2").Select
ActiveSheet.Paste

您可以写为

Sheets("1. SMARTnet ").Range("B9:C20694").Copy Sheets("Sheet1").Range("a2")

然后您将能够更好地找到错误

这也会运行得更快,因为它可以节省屏幕更新时间。