2017-08-02 54 views
1

我很新的VBA和我想要在這裏做的可能是完全瘋狂,但請溫柔與我:-)如果在for循環

所以有點背景的陳述。我有一個項目清單(TLD)和價目表。使用下面的代碼,如果項目的名稱,產品和期限是正確的(我希望這是有道理的),我試圖從每個項目的價格表中扣除價格。

當我運行它,我得到 「編譯錯誤:否則沒有如果」

Sub add_prices() 

    Dim startnumber As Long 
    Dim endnumber As Long 
    Dim TLD As String 
    Dim Listtld As String 

    endnumber = Sheets("Pricelist").Application.WorksheetFunction.CountF(Range("F2:F40000")) - 1 

    For startnumber = 0 To endnumber 

     TLD = Cells(3 + startnumber, 2) 
     Listtld = Sheets("pricelist").Cells(2 + startnumber, 7) 
     Product = Sheets("pricelist").Cells(2 + startnumber, 8) 
     Period = Sheets("pricelist").Cells(2 + startnumber, 9) 

     If TLD = Listtld Then 
      If Product = "auto renewal" Then 
       If Period = "1 year" Then 
        Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(2 + startnumber, 2) 
       Else 
        If Period = "2 years" Then 
         Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(3 + startnumber, 2) 
        End If 
       Else 
        If Period = "3 years" Then 
         Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(4 + startnumber, 2) 
        End If 
       Else 
        If Period = "4 years" Then 
         Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(5 + startnumber, 2) 
        End If 
       Else 
        If Period = "5 years" Then 
         Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(6 + startnumber, 2) 
        End If 
       End If 
      End If 
     End If 
    Next startnumber 
End Sub 

任何建議,將不勝感激

回答

2

改變那些Else s到ElseIf S:

If TLD = Listtld Then 
    If Product = "auto renewal" Then 
     If Period = "1 year" Then 
      Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(2 + startnumber, 2) 
     ElseIf Period = "2 years" Then 
      Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(3 + startnumber, 2) 
     ElseIf Period = "3 years" Then 
      Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(4 + startnumber, 2) 
     ElseIf Period = "4 years" Then 
      Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(5 + startnumber, 2) 
     ElseIf Period = "5 years" Then 
      Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(6 + startnumber, 2) 
     End If 
    End If 
End If 
+1

同時刪除結束時,如果在內部,如果的 –

1

肯定切換到Select Case,而不是多個If s:

Select Case Period 
    Case "1 year" 
     Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(2 + startnumber, 2) 

    Case "2 years" 
     Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(3 + startnumber, 2) 

    Case "3 years" 
     Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(4 + startnumber, 2) 

    Case "4 years" 
     Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(5 + startnumber, 2) 

    Case "5 years" 
     Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(6 + startnumber, 2) 

End Select 
0

這是固定的了:

Sub add_prices() 
    Dim startnumber As Long 
    Dim endnumber As Long 
    Dim TLD As String 
    Dim Listtld As String 
    Dim Product As String 
    Dim Period As String 

    endnumber = Sheets("Pricelist").Application.WorksheetFunction.CountF(Range("F2:F40000")) - 1 

    For startnumber = 0 To endnumber 

    TLD = Cells(3 + startnumber, 2) 
    Listtld = Sheets("pricelist").Cells(2 + startnumber, 7) 
    Product = Sheets("pricelist").Cells(2 + startnumber, 8) 
    Period = Sheets("pricelist").Cells(2 + startnumber, 9) 

    If TLD = Listtld Then 
     If Product = "auto renewal" Then 
      If Period = "1 year" Then 
       Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(2 + startnumber, 2) 
      ElseIf Period = "2 years" Then 
       Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(3 + startnumber, 2) 
      ElseIf Period = "3 years" Then 
       Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(4 + startnumber, 2) 
      ElseIf Period = "4 years" Then 
       Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(5 + startnumber, 2) 
      ElseIf Period = "5 years" Then 
       Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(6 + startnumber, 2) 
      End If 
     End If 
    End If 
    Next startnumber 
End Sub 
0

我毫不猶豫地添加其他的解決方案,但你可以縮短你的代碼

Sub add_prices() 

Dim startnumber As Long 
Dim endnumber As Long 
Dim TLD As String 
Dim Listtld As String 

endnumber = Sheets("Pricelist").Application.WorksheetFunction.CountF(Range("F2:F40000")) - 1 

For startnumber = 0 To endnumber 
    TLD = Cells(3 + startnumber, 2) 
    Listtld = Sheets("pricelist").Cells(2 + startnumber, 7) 
    Product = Sheets("pricelist").Cells(2 + startnumber, 8) 
    Period = Sheets("pricelist").Cells(2 + startnumber, 9) 

    If TLD = Listtld Then 
     If Product = "auto renewal" Then 
      Select Case Period 
       Case "1 year", "2 years", "3 years", "4 years", "5 years" 
        Cells(3 + startnumber, 3).Value = Sheets("pricelist").Cells(1 + startnumber + CLng(Left(Period, 1)), 2) 
       Case Else 
        'perhaps nothing to do here 
      End Select 
     End If 
    End If 
Next startnumber 

End Sub