Range.find方法查找具有特定colorindex的单元格

时间:2017-08-17 06:37:54

标签: excel vba find

我想找到具有相同colorIndex和范围的单元格 所以我录制了一个宏来查找带有颜色的单元格 并应用于这样的代码。 但它不起作用。

如何找到具有特定颜色指数的细胞?

For Each r In rngC
lcolorID = r.Offset(, 1).Interior.ColorIndex
     With wsD.UsedRange
          With Application.FindFormat.Interior
         .PatternColorIndex = xlAutomatic
          .Color = lcolorID
          End With
      Set c = .Find(What:="", After:=ActiveCell,  _ 
LookIn:=xlFormulas, LookAt:= xlPart, SearchOrder:=xlByRows, _ 
SearchDirection:=xlNext, MatchCase:=False, MatchByte:=False, _ 
SearchFormat:=True)

        If Not c Is Nothing Then
        Debug.Print c.Address
        End If
   End With
Next r

2 个答案:

答案 0 :(得分:1)

If Cell.Interior.ColorIndex = yourColorIndex Then 'using your color variable 
    'your actions here
End If

将上面的if条件放在for循环中,你可以遍历所有行,它应该可以工作!并使用您用于浏览工作表的循环,将当前正在处理的特定单元格替换为“单元格”。如果您的工作表名称为"Sheet",请使用:

Sheets("Sheet").Cell(rowindex, columnindex).Interior.ColorIndex 

rowindexcolumnindex是您正在循环的索引。

答案 1 :(得分:1)

Application.FindFormat.Clear之前尝试With Application.FindFormat.Interior

您可能已经设置了某些格式,这可能会阻止您匹配所需的单元格。