在现有If块中添加新的If语句

时间:2016-02-07 19:49:03

标签: excel vba excel-vba

我有这个代码比较A列和B列,如果A更大,则将1加到B列:

Sub test07()

    With Sheets("Sheet1")

        Dim LastRow As Long, i As Long

        LastRow = Cells(Rows.Count, "A").End(xlUp).Row

        For i = 12 To LastRow

            If Range("A" & i).Value > Range("B" & i).Value Then

                Range("B" & i).Value = Range("B" & i).Value + 1

            End If

        Next i

    End With

End Sub

我想再次添加相同的内容,但是使用C和D列,但是我遇到了语法错误,即:

Sub test07()

    With Sheets("Sheet1")

        Dim LastRow As Long, i As Long

        LastRow = Cells(Rows.Count, "A").End(xlUp).Row

        For i = 12 To LastRow

            If Range("A" & i).Value > Range("B" & i).Value Then

                Range("B" & i).Value = Range("B" & i).Value + 1

            If Range("C" & i).Value > Range("D" & i).Value Then

                Range("D" & i).Value = Range("D" & i).Value + 1

            End If

        Next i

    End With

End Sub

谁能看到我哪里出错了?非常感谢

3 个答案:

答案 0 :(得分:5)

正如评论中所提到的,您错过了End If。但是,您也没有充分利用使用With ... End With statement标识工作表时的显式父级。

Sub test07()
    Dim lastRow As Long, i As Long
    With Sheets("Sheet1")
        lastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        For i = 12 To lastRow
            If .Range("A" & i).Value > .Range("B" & i).Value Then
                .Range("B" & i).Value = .Range("B" & i).Value + 1
            End If   '<~~might have to be three lines down depending upon how you want your logic to flow
            If .Range("C" & i).Value > .Range("D" & i).Value Then
                .Range("D" & i).Value = .Range("D" & i).Value + 1
            End If
        Next i
    End With
End Sub

请注意使用.Range.Cells;不是RangeCells。前缀句点(又名句号)将范围和单元格与With ... End With中引用的工作表相关联。

回到If ... End If问题,如果您希望避免关闭If语句,可以使用它们,如下所示。

Sub test07()
    Dim lastRow As Long, i As Long
    With Sheets("Sheet1")
        lastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        For i = 12 To lastRow
            If .Range("A" & i).Value > .Range("B" & i).Value Then _
                .Range("B" & i).Value = .Range("B" & i).Value + 1
            If .Range("C" & i).Value > .Range("D" & i).Value Then _
                .Range("D" & i).Value = .Range("D" & i).Value + 1
        Next i
    End With
End Sub

此方法仅适用于If代码行之后的单个相关代码行。

答案 1 :(得分:1)

您也可以简单地复制/粘贴代码并修改

Range("A" & i) --> Range("A" & i).Offset(#rows,#cols)

或者更好的是,抛弃“Range”并使用带有两个迭代器和嵌套FOR循环的“Cells”......

With...

for i in {#rowStart} To {#rowEnd}
    for j in {#colstart} To {#Colend}
        .Cells(i,j).Value = {put stuff here}

使用Ubound(Range())计算数组中的元素并调整i&amp;的大小。 j等....

答案 2 :(得分:0)

你也应该熟悉ElseIf - ElseIf与做一个End相同如果后跟一个新的If语句除了它只在第一个If语句导致False时运行。即:假设A1 = 5,并且您想检查A1的值以确定B1的值。您可以通过两种方式检查值:

选项1,使用End If后跟新的If语句:

If Range("A1") > 3 Then 
    B1 = 2
End If
If Range("A1") > 4 Then 
    B1 = 1
End If 'Because A1 = 5, both If statements are True, and therefore B1 will equal 1, because that is the last line of code which affects it

选项2,使用ElseIf:

If Range("A1") > 3 Then 
    B1 = 2
ElseIf Range("A1") > 4 Then 
    B1 = 1
End If 'Because A1 = 5, the first If statement is True, the ElseIf statement never runs, and therefore B1 will equal 2, because that is the only line of code which affects it.

这两种方法都是有效的 - 您只需要了解您实际需要使用的逻辑路径。