在Col D

时间:2018-06-14 10:46:21

标签: excel-vba vba excel

在我的专栏D中,我有数字。通常,Col D的大多数行将具有单个数字,例如2或12或22,依此类推。但是在某些情况下它会有两个或更多的数字,例如2,4,12。单元格中的数字用逗号分隔。

示例:

       Col D
Row1    1
Row2    4
Row3    2,12
Row4    11,1
Row5    2,1
Row6    3
Row7    21
Row8    1,11,15
Row9    10,1,9
Row10   1,16

我该如何选择 Col D中的所有行数为1 即在上面的例子中,它将选择row1,row4,row5,row8,row9和row10

很多人

3 个答案:

答案 0 :(得分:2)

这样做:

Sub SelectRows()

    Dim lastRow As Long
    Dim rng As Range, rw As Range

    With ThisWorkbook.Sheets("test") ' change to your specific sheetname

        lastRow = .Cells.Find(What:="*", _
            SearchOrder:=xlRows, _
            SearchDirection:=xlPrevious, _
            LookIn:=xlValues).Row

        For Each rw In .Columns("D").Rows 
            If InStr(1, rw.Value, "1") > 0 Then
                If rng Is Nothing Then
                    Set rng = rw
                Else
                    Set rng = Union(rng, rw)
                End If
            End If
            If rw.Row = lastRow Then Exit For
        Next rw
    End With

    rng.Select

End Sub

编辑#1 这个改进的代码没有像早期版本那样的误报:

Sub SelectRows()

    Dim addBool As Boolean
    Dim lastRow As Long, lenTempStr As Long
    Dim rng As Range, rw As Range
    Dim tempStr As Variant

    With ThisWorkbook.Sheets("test") ' change to your specific sheetname

        lastRow = .Cells.Find(What:="*", _
            SearchOrder:=xlRows, _
            SearchDirection:=xlPrevious, _
            LookIn:=xlValues).Row

        For Each rw In .Columns("D").Rows 
            addBool = False
            tempStr = Replace(rw.Value, Chr(32), vbNullString)
            lenTempStr = Len(tempStr)

            If InStr(1, tempStr, "1") > 0 Then
                If lenTempStr = 1 Then
                    addBool = True
                ElseIf InStr(1, tempStr, ",1,") > 0 Then
                    addBool = True
                ElseIf Left(tempStr, 2) = "1," Then
                    addBool = True
                ElseIf Right(tempStr, 2) = ",1" Then
                    addBool = True
                End If

                If addBool Then
                    If rng Is Nothing Then
                        Set rng = rw
                    Else
                        Set rng = Union(rng, rw)
                    End If
                End If
            End If

            If rw.Row = lastRow Then Exit For
        Next rw
    End With

    rng.Select

End Sub

答案 1 :(得分:0)

Sub Maybe_A()
Dim c As Range, i As Long, a As String
a = ""
    For Each c In Range("D2:D" & Cells(Rows.Count, 4).End(xlUp).Row)
        For i = LBound(Split(c, ",")) To UBound(Split(c, ","))
            If Trim(Split(c, ",")(i)) = "1" Then
                If a = "" Then
                    a = c.Address(0, 0)
                        Else
                    a = a & "," & c.Address
                End If
                Exit For
            End If
        Next i
    Next c
Range(a).EntireRow.Select
End Sub

答案 2 :(得分:0)

许多人向你们所有人致意。非常感激。以下是我提供帮助的代码。不确定它有多好,如果能做到,虽然看起来有效。我之前没有看过select case true!

Sub FindN()
    Dim rC As Range, Rng As Range
    Dim sFind As String, fC1 As String, fC2 As String, fC3 As String, fC4 As String
    sFind = [d3].Value 'type the numeral to find in cell D3
    'find criteria - i think there are four outcomes
    fC1 = sFind: fC2 = sFind & ",*": fC3 = "*," & sFind & ",*": fC4 = "*," & sFind
    Rng = Range("d4:d14")

    Cells.EntireRow.Hidden = False
    For Each rC In Rng
        Select Case True
            Case rC.Value = Val(fC1)   'for numeric 1
                rC.EntireRow.Hidden = False
            Case rC.Value Like fC2
                rC.EntireRow.Hidden = False
            Case rC.Value Like fC3
                rC.EntireRow.Hidden = False
            Case rC.Value Like fC4
                rC.EntireRow.Hidden = False
            Case Else
                rC.EntireRow.Hidden = True
        End Select
    Next
End Sub