VBA中的FIND函数-对象不支持此属性或方法

时间:2019-05-15 07:11:29

标签: excel vba

无法在一张纸上运行FIND功能。

我想找到一些选定范围的单元格值并为其着色。
我收到错误消息

  

对象不支持此属性或方法

Sub coloring()
    ThisWorkbook.Sheets("VNF Placement").Activate

    Dim rg As Range

    Set rg = ThisWorkbook.Sheets("VNF Placement").Range("B11:CV500")
    lrow = ThisWorkbook.Sheets("VNF Placement").Cells(Rows.count, 105).End(xlUp).Row

    g = 3   'colour index
    i = 1

    For i = 2 To lrow
       For Each c In rg
           If ThisWorkbook.Sheets("VNF Placement").Find(Cells(i, 105), xlValues) Then
               c.Interior.ColorIndex = g
           End If
       Next

       g = g + 1
    Next

End Sub

1 个答案:

答案 0 :(得分:2)

您不应该使用Like来比较值。 Find是一个Range对象,它返回找到的值的范围。另外,您需要指定查找范围。

例如:

Dim rslt As Range, i As Long, lrow As Long

With ThisWorkbook.Sheets("VNF Placement")

    lrow = .Cells(.Rows.Count, "A").End(xlUp).Row

    For i = 2 To lrow
        Set rslt = .Range("A:A").Find(.Cells(i, 105), Lookat:=xlPart)
        If not rslt Is Nothing Then 
            MsgBox "The address of '" & .Cells(i, 105).Value & "' is '" & rslt.Address & "'", vbInformation
        End If
    Next

End With