excel VBA中的过程错误太大

时间:2014-08-23 19:37:03

标签: excel vba excel-vba excel-2013

我不习惯编写代码。我通常通过宏生成我的代码,我正面临这个问题。有人可以帮助我吗?

     Sub Macro1()
    '
    ' Macro1 Macro
    '
    ' Keyboard Shortcut: Ctrl+q
    '
    Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Rules").Select
Range("A2").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$A$307").RemoveDuplicates Columns:=1, Header:=xlYes
Sheets("Input").Select
ActiveWindow.LargeScroll Down:=-14
Range("A1").Select
Cells.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Sheets("Rules" _
    ).Range("A1:A2"), Unique:=False
ActiveCell.Offset(1, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Result").Select
Range("A2").Select
ActiveSheet.Paste
Selection.End(xlDown).Select
ActiveCell.Offset(3, 0).Range("A1").Select
Sheets("Rules").Select
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Sheets("Input").Select
Range("A1").Select
Cells.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Sheets("Rules" _
    ).Range("A1:A2"), Unique:=False
ActiveCell.Offset(2, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Result").Select
ActiveSheet.Paste
Selection.End(xlDown).Select
ActiveCell.Offset(3, 0).Range("A1").Select
Sheets("Rules").Select
Rows("2:2").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Sheets("Input").Select
Range("A1").Select
Cells.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Sheets("Rules" _
    ).Range("A1:A2"), Unique:=False
ActiveCell.Offset(2, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Result").Select
ActiveSheet.Paste
Selection.End(xlDown).Select
ActiveCell.Offset(3, 0).Range("A1").Select
Sheets("Rules").Select
Rows("2:2").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Sheets("Input").Select
Range("A1").Select
Cells.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Sheets("Rules" _
    ).Range("A1:A2"), Unique:=False
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Result").Select
ActiveSheet.Paste
End Sub

我想重复这些步骤50次,但是当我尝试复制/粘贴50次时,我收到错误消息“过程太大”。你能告诉我如何以较小的步骤做到这一点吗?

Range("A1").Select
Cells.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Sheets("Rules" _
    ).Range("A1:A2"), Unique:=False
ActiveCell.Offset(2, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Result").Select
ActiveSheet.Paste
Selection.End(xlDown).Select
ActiveCell.Offset(3, 0).Range("A1").Select
Sheets("Rules").Select
Rows("2:2").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Sheets("Input").Select

1 个答案:

答案 0 :(得分:2)

仅保存 您需要在宏中重复的步骤,例如Macro2

然后像这样创建一个新的Sub

Sub RepeatMacro2

' Beginning steps (will not be repeated)

For i = 1 to 50
    Macro2
Next i

' Final steps (will not be repeated)

End Sub

您可以在For之后复制/粘贴第一步(不要重复),然后在Next之后复制/粘贴最后一步(也不要重复)。

使用“宏”对话框调用RepeatMacro2