选择列中的所有负值

时间:2012-09-08 02:06:32

标签: vba

我从这个网站获得了以下代码,它选择了包含值“1”的H列中的所有单元格,但是我希望你可以帮我选择包含负值的H列中的所有单元格

Sub H()
    Dim TheSheet As Worksheet
    If TypeOf ActiveSheet Is Worksheet Then
        Set TheSheet = ActiveSheet
    Else
        Exit Sub
    End If
    Dim Row As Integer
    Dim CellsToSelect As String
    For Row = 1 To TheSheet.Range("H" & CStr(TheSheet.Rows.Count)).End(xlUp).Row
        If TheSheet.Range("H" & CStr(Row)).Value = 1 Then
            If CellsToSelect <> "" Then CellsToSelect = CellsToSelect & ","
            CellsToSelect = CellsToSelect & "H" & CStr(Row)
        End If
    Next Row
    TheSheet.Range(CellsToSelect).Select
End Sub

谢谢如果提前 诺尔

1 个答案:

答案 0 :(得分:0)

更改此行:If TheSheet.Range("H" & CStr(Row)).Value = 1 Then

对此:If TheSheet.Range("H" & CStr(Row)).Value < 0 Then

为了您的方便,这是整个子。

 Sub H()
    Dim TheSheet As Worksheet
    If TypeOf ActiveSheet Is Worksheet Then
        Set TheSheet = ActiveSheet
    Else
        Exit Sub
    End If
    Dim Row As Integer
    Dim CellsToSelect As String
    For Row = 1 To TheSheet.Range("H" & CStr(TheSheet.Rows.Count)).End(xlUp).Row
        If TheSheet.Range("H" & CStr(Row)).Value < 0 Then
            If CellsToSelect <> "" Then CellsToSelect = CellsToSelect & ","
            CellsToSelect = CellsToSelect & "H" & CStr(Row)
        End If
    Next Row
    If Len(CellsToSelect) <> 0 Then
        TheSheet.Range(CellsToSelect).Select
    End If
End Sub