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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22641904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:01:22  来源:igfitidea点击:

Excel VBA to select a range based on cell value

excelvba

提问by ganesh

I am new to 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

please make suitable corrections

请进行适当的更正

回答by Dmitry Pavliv

Try this one:

试试这个:

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