很好,我想再加一句:if rgCriteria.Rows.Count>1做一个判断就更好。 这样就可以按行和列引用了。
Function LinkIf$(ByVal rgCriteria As Range, ByVal Criteria As String, Optional ByVal rgLink As Range = Nothing, Optional ByVal lnkString As String = "")
On Error GoTo Errs
Dim I%, blLink As Boolean, rg As Range, hdo$, Crt As Variant
If rgLink Is Nothing Then Set rgLink = rgCriteria
If Criteria Like "=*" Then
hdo = "="
Criteria = Mid$(Criteria, 2)
ElseIf Criteria Like "<>*" Then
hdo = "<>"
Criteria = Mid$(Criteria, 3)
ElseIf Criteria Like "<=*" Then
hdo = "<="
Criteria = Mid$(Criteria, 3)
ElseIf Criteria Like ">=*" Then
hdo = ">="
Criteria = Mid$(Criteria, 3)
ElseIf Criteria Like ">*" Then
hdo = ">"
Criteria = Mid$(Criteria, 2)
ElseIf Criteria Like "<*" Then
hdo = "<"
Criteria = Mid$(Criteria, 2)
Else
hdo = "="
End If
I = 0
If rgCriteria.Rows.Count > 1 Then
For Each rg In rgCriteria
If (IsNumeric(rg) Or IsDate(rg)) And IsNumeric(Crt) And TypeName(rg.Value) <> "String" Then
Crt = Val(Criteria)
Else
Crt = Criteria
End If
Select Case hdo
Case "="
blLink = (rg = Crt)
Case "<>"
blLink = (rg <> Crt)
Case ">="
blLink = (rg >= Crt)
Case "<="
blLink = (rg <= Crt)
Case ">"
blLink = (rg > Crt)
Case "<"
blLink = (rg < Crt)
End Select
If blLink Then LinkIf = LinkIf & lnkString & rgLink.Cells(1).Offset(I, 0).Text
I = I + 1
Next rg
Else
For Each rg In rgCriteria
If (IsNumeric(rg) Or IsDate(rg)) And IsNumeric(Crt) And TypeName(rg.Value) <> "String" Then
Crt = Val(Criteria)
Else
Crt = Criteria
End If
Select Case hdo
Case "="
blLink = (rg = Crt)
Case "<>"
blLink = (rg <> Crt)
Case ">="
blLink = (rg >= Crt)
Case "<="
blLink = (rg <= Crt)
Case ">"
blLink = (rg > Crt)
Case "<"
blLink = (rg < Crt)
End Select
If blLink Then LinkIf = LinkIf & lnkString & rgLink.Cells(1).Offset(0, I).Text
I = I + 1
Next rg
End If
LinkIf = Mid$(LinkIf, Len(lnkString) + 1)
Exit Function
Errs:
Err.Clear
LinkIf = "#N/A"
End Function
[此贴子已经被作者于2006-3-30 13:06:43编辑过] |