2016-07-26 31 views
0

我不斷收到Next Without For錯誤,我看不出爲什麼。我想要做的是基本搜索西北地區的主工作表,複製該行的所有信息,然後將其粘貼到North West表格中。Excel Next Without

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 
+0

學會正確縮進代碼(在這裏和你自己的機器上),並且它將使這類問題,以圖更容易你自己。 –

+0

@ user2771898在下面看到我的回答,它是在表單之間複製和粘貼,而不是始終選擇表單。 –

+0

感謝提示肯。謝謝Shai,我會嘗試你的方法 - 非常感謝你的投入和時間,非常感謝。 – user2771898

回答

3

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將工作

0

代碼的另一種選擇,就是忌用Sheets.Select並聲明表變量以避免可能的錯誤喲你可能會使用ActiveSheet

我假設你想要的工作表之間的多個複製粘貼,如果細胞在E列MASTERSHEET等於「西北」。下面

查找代碼,讓我知道是它的工作對你

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