查找最后一行直到下一个突出显示的单元格

时间:2019-09-05 02:20:09

标签: excel vba

我试图找到最后一行直到下一个突出显示的单元格并清除范围。

Range("B2").End(xlDown)不起作用,我在xlCellTypeSameFormatConditions下找到了一个叫做SpecialCells的东西,但不确定如何应用。

也许有更好的方法?
结果应仅清除范围B2:B7

enter image description here

好,将这两种解决方案合并为一个,我就这样

Private Sub WorkSheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
  If Not Intersect(Target, Range("A1")) Is Nothing Then

  Dim rngCheck, rngCell As Range

  Set rngCheck = ActiveSheet.Range("B2:B" & Cells(2, 2).End(xlDown).Row)
    For Each rngCell In rngCheck
      If rngCell.Interior.Pattern = xlNone Or rngCell.Value = "" Then rngCell.Value = ""
    Next
  Set rngCheck = Nothing

  End If
End Sub

因此,基本上,当“ A1”中的值更改时,将触发清除。
相同的代码在Module下有效,但不适用于WorkSheet_Change

2 个答案:

答案 0 :(得分:1)

尝试此操作,请注意没有异常或错误处理。无论颜色是什么,只要碰到突出显示的单元格,该操作就会停止,并且不会删除突出显示的单元格之间的非突出显示的单元格。

Sub MoveToNextHighlightedCell()
  Do Until Not ActiveCell.Interior.Pattern = xlNone Or ActiveCell.Value = ""
    ActiveCell.Value = ""
    ActiveCell.Offset(1, 0).Select
  Loop
End Sub

或者使用它,除非缺少值,它不会停止。根据@Mikku的评论进行了更新。

Sub MoveToNextHighlightedCell()
  Dim rngCheck, rngCell As Range
  Set rngCheck = ActiveSheet.Range(ActiveCell, ActiveCell.End(xlDown))
    For Each rngCell In rngCheck
      If rngCell.Interior.Pattern = xlNone Or rngCell.Value = "" Then rngCell.Value = ""
    Next
  Set rngCheck = Nothing
End Sub

答案 1 :(得分:1)

您可以尝试:

enter image description here

Pair.of(1, 2).toString(); // prints (1,2)
Pair.of(1, 2).toString("%s_%s"); // prints 1_2

enter image description here


如果要在换页事件上运行代码,请尝试以下操作:

Sub test()

Dim rng As Range

With Application.FindFormat
    .Clear
    .Interior.Color = vbWhite
End With

With ThisWorkbook.Sheets("Sheet1") 'Change to correct sheetname
    Set rng = .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)
    rng.Cells.Replace What:="*", Replacement:="", SearchFormat:=True
End With

End Sub