如果使用UDF大于和小于特定值,则返回值

时间:2017-01-29 22:43:41

标签: excel-vba excel-udf vba excel

我目前正在研究UDF,如果它大于或小于特定值,则返回并连接标头。我在Excel-Vba中并不是很擅长,到目前为止我得到的是这个可怜的代码,我无法理解。如果有人可以提供帮助,我会非常感激。这是我到目前为止所得到的:

Public Function greaterLessValue(Rng As Range, Rng2 As Range, greater, less)

Dim rngArr() As Variant
rngArr = Rng.value

For i = 1 To UBound(rngArr, 1)
    For j = 1 To UBound(rngArr, 2)
        If rngArr(i, j) = xVal Then
            For k = 1 To UBound(rngArr, 2)
                If rngArr(i, k) = Yval Then countRowAsso = countRowAsso + 1
            Next k
        End If
    Next j
Next i

End Function

如果该值大于或等于5且小于等于10,则应返回相同的示例,该示例连接包含同一列中特定值的Header:

3 个答案:

答案 0 :(得分:2)

试试这个UDF:

(@1 varchar(8000))SELECT * FROM [Production].[Product] WHERE [Name]=@1

<强>用法

在单元格Public Function greaterLess(values As Range, header As Range, a, b) As String Dim cel As Range For Each cel In values If cel.value >= a And cel.value <= b Then greaterLess = _ greaterLess & header.Cells(1, cel.Column - values.Column + 1) & ", " Next End Function 中输入以下公式:

N2

然后复制=greaterLess(B2:M2, B$1:M$1, 5, 10) ,选择N2并粘贴。

答案 1 :(得分:2)

如果您有Office 365 Excel,则可以使用数组公式执行此操作:

=TEXTJOIN(", ",TRUE,IF((B2:M2>=5)*(B2:M2<=10),B$1:M$1,""))

作为数组公式,需要使用Ctrl-Shift-Enter输入,而不是在退出编辑模式时输入。如果操作正确,则Excel会将{}放在公式周围。

如果您没有Office 365 Excel,则可以使用此UDF来模拟该功能。

Function TEXTJOIN(delim As String, skipblank As Boolean, arr)
    Dim d As Long
    Dim c As Long
    Dim arr2()
    Dim t As Long, y As Long
    t = -1
    y = -1
    If TypeName(arr) = "Range" Then
        arr2 = arr.Value
    Else
        arr2 = arr
    End If
    On Error Resume Next
    t = UBound(arr2, 2)
    y = UBound(arr2, 1)
    On Error GoTo 0

    If t >= 0 And y >= 0 Then
        For c = LBound(arr2, 1) To UBound(arr2, 1)
            For d = LBound(arr2, 1) To UBound(arr2, 2)
                If arr2(c, d) <> "" Or Not skipblank Then
                    TEXTJOIN = TEXTJOIN & arr2(c, d) & delim
                End If
            Next d
        Next c
    Else
        For c = LBound(arr2) To UBound(arr2)
            If arr2(c) <> "" Or Not skipblank Then
                TEXTJOIN = TEXTJOIN & arr2(c) & delim
            End If
        Next c
    End If
    TEXTJOIN = Left(TEXTJOIN, Len(TEXTJOIN) - Len(delim))
End Function

公式相同,仍然使用Ctrl-Shift-Enter而不是Enter输入。

答案 2 :(得分:2)

A.S.H确定了范围包含相同数量的元素/值/单元格。我使用了不同的方法。

Option Explicit

Public Function greaterLessValue(rng1 As Range, rng2 As Range, _
                                 greater As Double, lesser As Double)

    Dim i As Long, j As Long
    Dim rngArr1 As Variant, rngArr2 As Variant

    rngArr1 = rng1.Value2
    rngArr2 = rng2.Value2
    greaterLessValue = ""

    'use for showing array extents
    'delete or comment out when function works
    Debug.Print LBound(rngArr1, 1) & " to " & UBound(rngArr1, 1)
    Debug.Print LBound(rngArr1, 2) & " to " & UBound(rngArr1, 2)

    'used to ensure that the ranges hold the same number of columns
    'only affects the 2nd rank when used with Preserve
    ReDim Preserve rngArr2(LBound(rngArr1, 1) To UBound(rngArr1, 1), _
                            LBound(rngArr1, 2) To UBound(rngArr1, 2))

    For i = LBound(rngArr1, 1) To UBound(rngArr1, 1)
        For j = LBound(rngArr1, 2) To UBound(rngArr1, 2)
            If IsNumeric(rngArr2(i, j)) Then
                If rngArr2(i, j) >= greater And rngArr2(i, j) <= lesser Then
                    greaterLessValue = greaterLessValue & _
                                       IIf(CBool(Len(greaterLessValue)), ", ", vbNullString) & _
                                       rngArr1(i, j)
                End If
            End If
        Next j
    Next i

End Function

语法如下图所示:

enter image description here