Excel:我应该编写什么代码来直接在具有特定值的单元格上方选择多个单元格?

时间:2011-07-01 12:31:28

标签: excel vba

我按特定顺序排列了一列数字。在该列中,一些数字是99999999.我需要选择直接高于此数字的单元格。这是一个例子,我需要选择所有突出显示为粗体的单元格:

  • 63012097
  • 63012097
  • 63012097
  • 63133638
  • 63133638
  • 99999999
  • 99999999
  • 63048742
  • 63048742
  • 63020783
  • 63066755
  • 63167680
  • 99999999
  • 99999999
  • 63033618
  • 63033618
  • 63033618
  • 63033618
  • 63033618
  • 63033618
  • 63033618
  • 63033618
  • 63033618
  • 63033618
  • 99999999
  • 99999999
  • 63005597
  • 63005597
  • 99999999
  • 99999999
  • 63099456
  • 63099456
  • 63099456
  • 63099456
  • 63099456
  • 99999999
  • 99999999
  • 63029683
  • 63029683

我只想将数据显示为列,因此忽略项目符号;)

我有723,950行,所以也不可能手动完成。有人可以帮忙吗?

谢谢! :d

3 个答案:

答案 0 :(得分:3)

一旦您选择了细胞,我不确定您想要对细胞做什么。如果您只想在示例中突出显示它们,则可以使用Excel的条件格式设置功能。

只需选择数据列即可。在Excel 2007中,转到Home - >条件格式 - >新规则....从那里,选择“使用公式确定要格式化的单元格”。如果A1是列中的第一个条目,请输入以下公式:

=AND(OFFSET(A1,1,0)=99999999,A1<>99999999)

将格式设置为粗体或您喜欢的任何其他高光。

答案 1 :(得分:3)

您可能不想真正选择它们。您可能希望对它们执行其他操作,这将更改此代码。但是这会选择它们。

Sub Select999()

    Dim rFound As Range
    Dim sFirstAdd As String
    Dim rSelect As Range
    Dim rSearch As Range

    Const lFIND As Long = 99999999

    Set rSearch = Sheet1.Columns(1)
    Set rFound = rSearch.Find(lFIND, , xlValues, xlWhole)

    If Not rFound Is Nothing Then
        sFirstAdd = rFound.Address
        Do
            If rFound.Row > 1 Then
                If rFound.Offset(-1, 0).Value <> lFIND Then
                    If rSelect Is Nothing Then
                        Set rSelect = rFound.Offset(-1, 0)
                    Else
                        Set rSelect = Union(rSelect, rFound.Offset(-1, 0))
                    End If
                End If
            End If

            Set rFound = rSearch.FindNext(rFound)

        Loop Until rFound.Address = sFirstAdd
    End If

    rSelect.Select

End Sub

答案 2 :(得分:1)

我会用这样的东西。可能有更有效的方法......

Sub SelectAbove99999999()

    Dim cell As Range
    Dim sel As String
    Dim above As String

    ' initialize
    Set cell = Range("A1")
    sel = ""

    ' do until a blank cell
    While cell.Text <> ""

        ' coords of the cell above this
        above = "A" & (cell.Row - 1)

        ' if this cell contains the key value
        If cell = "99999999" Then

            ' if above there isn't the key value
            If Range(above) <> "99999999" Then

                ' add this cell to the interesting ones
                sel = sel & above & ","

            End If

        End If

        ' next cell
        Set cell = cell.Offset(1, 0)

    Wend

    ' strip last comma
    sel = Left(sel, Len(sel) - 1)

    ' select
    Range(sel).Select

End Sub

注意:它假设值列在A列中,并以空白单元格结束。