在UDF函数vba中调用子例程

时间:2015-10-28 16:30:55

标签: excel vba call subroutine udf

请帮忙 我试图在一个函数内调用一个sub但是我无法达到我的目标,这里是我的代码,这首先是UDF

Public Function ColorIfExist(ValueToSeek As String, db As Range) As String
    'this functions is suppose to call the ColorThem subroutine but returns a #VALUE! error
    If Not db.Find(ValueToSeek, LookIn:=xlValues) Is Nothing Then
        Call ColorThem(ValueToSeek, db)
        ColorIfExist = "exists"
    Else
        ColorIfExist = "does not exist"
    End If
End Function

此部分是我想要调用的Sub

Public Sub ColorThem(ValueToSeek As String, db As Range)
    'this subroutine color the cells that be equal to the value to seek
    Dim c As Range
    Dim firstAddress As Variant
    Set c = db.Find(ValueToSeek, LookIn:=xlValues)
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            c.Interior.Color = RGB(0, 0, 100)
            Set c = db.FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
End Sub

我希望收到此函数的是一个文本,说明在范围DB的任何单元格中是否存在ValueToSeek,并且通过调用子ColorThem,为包含ValueToSeek的单元格着色处理范围DB

最好的问候

0 个答案:

没有答案