2014-03-05 48 views
0

爲什麼這不起作用?基本上我有一個單元格中的列表,我想分割以「sec」結尾的字符串,而不是將它們複製到不同的列中。For Loops問題

Sub test_if() 
    For i = 1 To 300 
     Cells(i, 2).Select 
     If Right(Cells(i, 2), 3) = "SEC" Then 
     ActiveCell.Select 
     Selection.Copy 
     Cells(i, 3).Select 
     ActiveSheet.Paste 
     End If 

     If Right(Cells(i, 2), 3) <> "SEC" Then 
     ActiveCell.Select 
     Selection.Copy 
     'Cells(i, 4).Select 
     ActiveCell.Offset(i - 1, 2).Select 
     ActiveSheet.Paste 
     End If 
    Next i  
    Cells(1, 1).Select 
End Sub 

回答

1

試試這個:

Sub test_if() 
    Dim i As Integer 

    For i = 1 To 300 
     With Cells(i, 2) 
      If UCase(Right(.Value, 3)) = "SEC" Then 
       .Offset(, 1).Value = .Value 
      Else 
       .Offset(i - 1, 2).Value = .Value 
      End If 
     End With 
    Next i 
End Sub 

,也請大家讀,how to avoid using Select/Active statements