如果大于或小于使用 UDF 的特定值,则返回值
Returning values if it is greater than and less than to a specific value using UDF
我目前正在研究一个 UDF,它 return 并连接 headers 如果它大于和小于特定值。我在 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,则它应该 return 与下面相同的示例连接同一列中包含特定值的 Header:
试试这个 UDF:
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:M, 5, 10)
然后复制 N2
、select N3:N7
并粘贴。
如果您有 Office 365 Excel,您可以使用数组公式执行此操作:
=TEXTJOIN(", ",TRUE,IF((B2:M2>=5)*(B2:M2<=10),B:M,""))
作为数组公式,退出编辑模式时需要输入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 输入。
A.S.H 在确认范围包含相同数量的 elements/values/cells 时提出了一个很好的观点。我使用了不同的方法。
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
语法如下图:
我目前正在研究一个 UDF,它 return 并连接 headers 如果它大于和小于特定值。我在 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,则它应该 return 与下面相同的示例连接同一列中包含特定值的 Header:
试试这个 UDF:
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:M, 5, 10)
然后复制 N2
、select N3:N7
并粘贴。
如果您有 Office 365 Excel,您可以使用数组公式执行此操作:
=TEXTJOIN(", ",TRUE,IF((B2:M2>=5)*(B2:M2<=10),B:M,""))
作为数组公式,退出编辑模式时需要输入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 输入。
A.S.H 在确认范围包含相同数量的 elements/values/cells 时提出了一个很好的观点。我使用了不同的方法。
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
语法如下图: