Excel:我应该写什么代码来select具有特定值的单元格上方的多个单元格?

我有一个特定的顺序数字列。 在那一栏中,一些数字是99999999.我需要select这个数字正上方的单元格。 这里是一个例子,我需要select所有加粗的单元格:

  • 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

一旦你select了这些单元格,我不确定你想要做什么。 如果您只是想让它们像您的示例中突出显示,则可以使用Excel的条件格式化function。

只需select数据列。 在Excel 2007中,转到主页 – >条件格式 – >新规则….从那里,select“使用公式确定要格式化的单元格”。 如果A1是列中的第一个条目,请input以下公式:

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

将格式设置为粗体或其他任何你想要的高光。

你可能不太想select它们。 你可能想要做些别的事情,这会改变这个代码。 但是这会select他们。

 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 

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

 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中列出,并由一个空白单元终止。