【问题标题】:If Statement in loop - Else, Else If循环中的 If 语句 - Else, Else If
【发布时间】:2015-10-28 03:34:53
【问题描述】:

我想知道是否有人可以通过以下简单代码提供帮助:

For Each cell In Range(Cells(2, 5), Cells(lastrow1, 5))
    If(cell.offset(, -3)=cell.offset(1,-3)) then cell.value = cell.offset(,-1),else if(cell.offset(, -3)=cell.offset(-1,-3),cell.value = 0,else     cell.valye = cell.offset(,-1))
Next

我基本上是在尝试复制 if 语句,根据 IF 语句是否为真,给出 3 种不同的结果。

【问题讨论】:

  • 你的代码不行吗?

标签: vba loops if-statement


【解决方案1】:

试试这个;

    If (cell.offset(, -3) = cell.offset(1, -3)) Then
        cell.value = cell.offset(, -1)
    ElseIf (cell.offset(, -3) = cell.offset(-1, -3)) Then
            cell.value = 0
    Else
        cell.valye = cell.offset(, -1)
    End If

【讨论】:

    【解决方案2】:

    如果您不了解有关命令语法的在线帮助,您应该熟悉它。

    例如here

    一般是here

    For Each cell In Range(Cells(2, 5), Cells(lastrow1, 5))
    
        If cell.Offset(, -3) = cell.Offset(1, -3) Then
                cell.Value = cell.Offset(, -1)
    
        ElseIf cell.Offset(, -3) = cell.Offset(-1, -3) Then
    
            cell.Value = 0
    
        Else
    
            cell.Value = cell.Offset(, -1)
    
        End If
    Next
    

    【讨论】:

      最近更新 更多