在列中查找值并选择同一行中的单元格

时间:2015-09-23 19:04:08

标签: vba excel-vba excel

我一直在使用代码来查找F列中的值“SIM”并选择A列和B列中同一行中的所有单元格。但是,它有2个“SIM”,所以它应该选择2行中的单元格A和B,但它只能在一行中选择!有人能帮我吗? 感谢

Sub teste()

'Alterar todo mes

Dim TR As Long, i As Long

Worksheets("Format2").Activate

'Alterar todo mes

TR = Range("F" & Rows.Count).End(xlUp).Row

For i = TR To 1 Step -1
    If (Range("F" & i)) = "SIM" Then
    Rows(i).Select
ActiveCell.EntireRow.Range("A1:b1").Select
    End If
Next i

End Sub

1 个答案:

答案 0 :(得分:0)

测试:

Sub teste()

    Dim TR As Long, i As Long, rng As Range
    Dim sht as WorkSheet

    Set sht = Worksheets("Format2")
    sht.Activate
    TR = sht.Range("F" & Rows.Count).End(xlUp).Row

    For i = TR To 1 Step -1
        If sht.Range("F" & i).Value = "SIM" Then
            If rng Is Nothing then
                Set rng = sht.Cells(i, 1).Resize(1,2)
            Else
                Set rng = Application.Union(rng, sht.Cells(i, 1).Resize(1,2))
            End If
        End If
    Next i

    If Not rng Is Nothing Then rng.Select
End Sub