Excel没有用

我一直在得到一个Next Without For错误,我不明白为什么。 我想要做的是基本search北西方的主要工作表,复制该行中的所有信息,然后将其粘贴到西北工作表。

Sub NorthWest() Sheets("MASTERSHEET").Select LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row For i = 2 To LastRow If Range("E" And i).Value = "North West" Then Rows(i).Select Selection.Copy Sheets("North West").Select erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row Next i ActiveSheet.Cells(erow, 1).Select ActiveSheet.Paste Application.CutCopyMode = False End If End Sub 

If没有End if

纠正代码

 If Range("E" And i).Value = "North West" Then Rows(i).Select Selection.Copy Sheets("North West").Select erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row End If 

Next将工作

代码的另一个选项是避免使用 Sheets.Select并声明工作表variables,以避免在使用ActiveSheet可能遇到的错误。

如果MASTERSHEET中的列E中的单元格等于“ North West ”,我认为您需要在工作表之间使用多个复制粘贴。

find下面的代码,让我知道它是为你工作

 Option Explicit Sub NorthWest() Dim sht_Master As Worksheet Dim sht_NorthWest As Worksheet Dim LastRow As Long Dim lrow As Long Dim erow As Long ' set sht objects to avoid possible errors if using Active sheet Set sht_Master = ThisWorkbook.Sheets("MASTERSHEET") Set sht_NorthWest = ThisWorkbook.Sheets("North West") LastRow = sht_Master.Range("A" & sht_Master.Rows.Count).End(xlUp).Row For lrow = 2 To LastRow ' I assumed you want to copy and past between sheets inside the For loop If sht_Master.Range("E" & lrow).Value = "North West" Then erow = sht_NorthWest.Cells(sht_NorthWest.Rows.Count, 1).End(xlUp).Offset(1, 0).Row ' copy and paste between sheets without using Select sht_Master.Rows(lrow).Copy _ Destination:=sht_NorthWest.Range("A" & erow) End If Next lrow End Sub