VBA代码中的错误-无效的下一个控制变量参考

时间:2019-10-07 05:00:44

标签: excel vba

我有以下代码返回错误:无效的下一个控制变量引用。谁能指出我的错误之处以及如何即兴演奏?该代码是基于某个特定关键字从单元格A2开始提取行

Sub Macro1()
    Dim DVariable As Date
    Dim RngFind As Range
    Dim MonthNo, YearNo As Integer
    Dim StartDate, EndDate As Date
    Dim PasteCell As Range
    Dim M As Long, i As Long, j As Long

    Application.DisplayAlerts = False
    Sheets("Report").Select
    Set ws1 = ThisWorkbook.Sheets.Add(After:= _
            ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    ws1.Name = "NEW"
    Sheets("Macro").Select
    For M = 2 To Cells(Rows.Count, "A").End(xlUp).Row
        Sheets("Macro").Select
            With Sheets("By Trader")
    'loop column N until last cell with value (not entire column)
                For Each Cell In .Range("N1:N" & .Cells(.Rows.Count, "N").End(xlUp).Row)
                    If Cell.Value = M Then
             ' Copy>>Paste in 1-line (no need to use Select)
                    .Rows(Cell.Row).Copy Destination:=Sheets("NEW").Rows(Cell.Row)
                    End If
                Next
            End With
     Next
End Sub

0 个答案:

没有答案