if语句里面的for循环

时间:2017-08-02 13:30:41

标签: excel vba excel-vba

我对VBA非常陌生,我在这里尝试做的事情可能完全疯狂,但请温柔地对待我: - )

所以有点背景知识。我有一个项目清单(TLD' s)和价目表。使用下面的代码,如果项目的名称,产品和期限是正确的,我试图从每个项目的价格表中提取价格(我希望这是有道理的)。

当我运行它时,我得到"编译错误:否则没有If"

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

非常感谢任何建议

4 个答案:

答案 0 :(得分:2)

if($_SESSION['laatstbekeken'] != ''){ $result = array_unique(array_reverse($_SESSION['laatstbekeken'])); $prods = implode(",", array_slice($result,0,3)); } 更改为Else s:

ElseIf

答案 1 :(得分:1)

绝对切换到Select Case,而不是多个If

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

答案 2 :(得分: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

答案 3 :(得分: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