如何返回函数的结果?

时间:2017-09-30 20:09:33

标签: excel-vba function return vba excel

我使用concatenateif作为函数开发代码,但是当我尝试返回返回值时,错误424出现。

有人可以帮助我吗?

Sub macro()
    Dim i As Long
    Dim LastRow As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    compareRange = Range("A2:A" & LastRow)
    stringsRange = Range("F2:F" & LastRow)
    For i = 2 To LastRow
    atributo = Worksheets("BOMs").Range("A" & i)
    Dim data As String
    data = ConcatIf(compareRange, atributo, stringsRange)'<<<return 
    Next i
End Sub

Function ConcatIf(ByVal compareRange As Range, ByVal xCriteria As Variant, Optional ByVal stringsRange As Range, _
    Optional Delimiter As String, Optional NoDuplicates As Boolean) As String

    Dim i As Long, j As Long
    With compareRange.Parent
        Set compareRange = Application.Intersect(compareRange, Range(.UsedRange, .Range("a1")))
    End With
    If compareRange Is Nothing Then Exit Function
    If stringsRange Is Nothing Then Set stringsRange = compareRange
    Set stringsRange = compareRange.Offset(stringsRange.Row - compareRange.Row, _
    stringsRange.Column - compareRange.Column)

    For i = 1 To compareRange.Rows.Count
        For j = 1 To compareRange.Columns.Count
            If (Application.CountIf(compareRange.Cells(i, j), xCriteria) = 1) Then
                If InStr(ConcatIf, Delimiter & CStr(stringsRange.Cells(i, j))) <> 0 Imp Not (NoDuplicates) Then
                    ConcatIf = ConcatIf & Delimiter & CStr(stringsRange.Cells(i, j))
                End If
            End If
        Next j
    Next i
    ConcatIf = Mid(ConcatIf, Len(Delimiter) + 1)
End Function

0 个答案:

没有答案