2017-08-02 55 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