在If blook之后,如果值为true,则不会选择整行

时间:2016-05-21 12:49:48

标签: excel-vba vba excel

我需要在值为true时选择整个最后一行,但它不起作用。帮我。

  Private Sub CommandButton4_Click()
     Dim erow, lastrow, i As Long
     lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
     For i = 2 To lastrow
     If Cells(i, 1) = Date And Cells(i, 2) = "Sales" Then
     Rows(lastrow).Select
    Selection.Copy
    End If
    Next
    End Sub

1 个答案:

答案 0 :(得分:0)

我只用了很小的改动就运行了你的代码:

Private Sub CommandButton4_Click()
    Dim erow As Long, lastrow As Long, i As Long

    lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

    For i = 2 To lastrow
        If Cells(i, 1) = Date And Cells(i, 2) = "Sales" Then
            Rows(lastrow).Select
            Selection.Copy
        End If
    Next
End Sub

它有效!

如您所见,它在第7行找到 True 并复制了最后一行(第21行)

enter image description here