Excel VBA根据单元格值选择范围

时间:2014-03-25 17:20:46

标签: excel vba

我是VBA的新手。

For each cell in columns("c:c")
If cell.value = "TRUE" Then
'vba is required for  selecting corresponding cells in columns A and B 
Next cell
Else:
exit sub
End if
end sub

请做出适当的更正

1 个答案:

答案 0 :(得分:4)

试试这个:

Sub test()
    Dim lastrow As Long
    Dim c As Range, rng As Range
    'change Sheet1 to suit
    With ThisWorkbook.Worksheets("Sheet1")
        lastrow = .Cells(.Rows.Count, "C").End(xlUp).Row
        For Each c In .Range("C1:C" & lastrow)
            If UCase(c.Text) = "FALSE" Then
                If rng Is Nothing Then
                    Set rng = .Range("A" & c.Row).Resize(, 2)
                Else
                    Set rng = Union(rng, .Range("A" & c.Row).Resize(, 2))
                End If
            End If
        Next c
    End With

    If Not rng Is Nothing Then rng.Select
End Sub