Excel-匹配数据并写入另一列
我想做一些匹配的数据显示在第三。 我有一列文件夹,第二列显示哪些用户有权访问该文件夹,因此:Excel-匹配数据并写入另一列,excel,excel-formula,vba,Excel,Excel Formula,Vba,我想做一些匹配的数据显示在第三。 我有一列文件夹,第二列显示哪些用户有权访问该文件夹,因此: Column1 Column2 Folder1 User1 Folder1 User2 Folder2 User1 Folder3 User1 我有一个要搜索的用户列表(我根据第2列创建了一个“唯一”列表),并希望在第2列和第3列中找到匹配项,将用户与用分隔符分隔的相应文件夹放在一起。它不必是第3列,甚至可以是另一张表,数据是第3列,这是我完成后唯一需要的数据。所以第3列看起来像: User1|Fol
Column1 Column2
Folder1 User1
Folder1 User2
Folder2 User1
Folder3 User1
我有一个要搜索的用户列表(我根据第2列创建了一个“唯一”列表),并希望在第2列和第3列中找到匹配项,将用户与用分隔符分隔的相应文件夹放在一起。它不必是第3列,甚至可以是另一张表,数据是第3列,这是我完成后唯一需要的数据。所以第3列看起来像:
User1|Folder1|Folder2|Folder3
User2|Folder1
这有意义吗
谢谢你能给我的帮助。
Scott使用下面包含的芯片UDF,您可以在目标单元格上放置以下公式:
=TRIM(stringconcat(" | ",IF(B:B="User1",A:A,"")))
这将输出与“User1”的“|”分隔符连接的所有文件夹。干杯
StringConcat代码:
Function StringConcat(Sep As String, ParamArray Args()) As Variant
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' StringConcat
' By Chip Pearson, chip@cpearson.com, www.cpearson.com
' www.cpearson.com/Excel/stringconcatenation.aspx
' This function concatenates all the elements in the Args array,
' delimited by the Sep character, into a single string. This function
' can be used in an array formula. There is a VBA imposed limit that
' a string in a passed in array (e.g., calling this function from
' an array formula in a worksheet cell) must be less than 256 characters.
' See the comments at STRING TOO LONG HANDLING for details.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim S As String
Dim N As Long
Dim M As Long
Dim R As Range
Dim NumDims As Long
Dim LB As Long
Dim IsArrayAlloc As Boolean
'''''''''''''''''''''''''''''''''''''''''''
' If no parameters were passed in, return
' vbNullString.
'''''''''''''''''''''''''''''''''''''''''''
If UBound(Args) - LBound(Args) + 1 = 0 Then
StringConcat = vbNullString
Exit Function
End If
For N = LBound(Args) To UBound(Args)
''''''''''''''''''''''''''''''''''''''''''''''''
' Loop through the Args
''''''''''''''''''''''''''''''''''''''''''''''''
If IsObject(Args(N)) = True Then
'''''''''''''''''''''''''''''''''''''
' OBJECT
' If we have an object, ensure it
' it a Range. The Range object
' is the only type of object we'll
' work with. Anything else causes
' a #VALUE error.
''''''''''''''''''''''''''''''''''''
If TypeOf Args(N) Is Excel.Range Then
'''''''''''''''''''''''''''''''''''''''''
' If it is a Range, loop through the
' cells and create append the elements
' to the string S.
'''''''''''''''''''''''''''''''''''''''''
For Each R In Args(N).Cells
If Len(R.Text) > 0 Then
S = S & R.Text & Sep
End If
Next R
Else
'''''''''''''''''''''''''''''''''
' Unsupported object type. Return
' a #VALUE error.
'''''''''''''''''''''''''''''''''
StringConcat = CVErr(xlErrValue)
Exit Function
End If
ElseIf IsArray(Args(N)) = True Then
'''''''''''''''''''''''''''''''''''''
' ARRAY
' If Args(N) is an array, ensure it
' is an allocated array.
'''''''''''''''''''''''''''''''''''''
IsArrayAlloc = (Not IsError(LBound(Args(N))) And _
(LBound(Args(N)) <= UBound(Args(N))))
If IsArrayAlloc = True Then
''''''''''''''''''''''''''''''''''''
' The array is allocated. Determine
' the number of dimensions of the
' array.
'''''''''''''''''''''''''''''''''''''
NumDims = 1
On Error Resume Next
Err.Clear
NumDims = 1
Do Until Err.Number <> 0
LB = LBound(Args(N), NumDims)
If Err.Number = 0 Then
NumDims = NumDims + 1
Else
NumDims = NumDims - 1
End If
Loop
On Error GoTo 0
Err.Clear
''''''''''''''''''''''''''''''''''
' The array must have either
' one or two dimensions. Greater
' that two caues a #VALUE error.
''''''''''''''''''''''''''''''''''
If NumDims > 2 Then
StringConcat = CVErr(xlErrValue)
Exit Function
End If
If NumDims = 1 Then
For M = LBound(Args(N)) To UBound(Args(N))
If Args(N)(M) <> vbNullString Then
S = S & Args(N)(M) & Sep
End If
Next M
Else
''''''''''''''''''''''''''''''''''''''''''''''''
' STRING TOO LONG HANDLING
' Here, the error handler must be set to either
' On Error GoTo ContinueLoop
' or
' On Error GoTo ErrH
' If you use ErrH, then any error, including
' a string too long error, will cause the function
' to return #VALUE and quit. If you use ContinueLoop,
' the problematic value is ignored and not included
' in the result, and the result is the concatenation
' of all non-error values in the input. This code is
' used in the case that an input string is longer than
' 255 characters.
''''''''''''''''''''''''''''''''''''''''''''''''
On Error GoTo ContinueLoop
'On Error GoTo ErrH
Err.Clear
For M = LBound(Args(N), 1) To UBound(Args(N), 1)
If Args(N)(M, 1) <> vbNullString Then
S = S & Args(N)(M, 1) & Sep
End If
Next M
Err.Clear
M = LBound(Args(N), 2)
If Err.Number = 0 Then
For M = LBound(Args(N), 2) To UBound(Args(N), 2)
If Args(N)(M, 2) <> vbNullString Then
S = S & Args(N)(M, 2) & Sep
End If
Next M
End If
On Error GoTo ErrH:
End If
Else
If Args(N) <> vbNullString Then
S = S & Args(N) & Sep
End If
End If
Else
On Error Resume Next
If Args(N) <> vbNullString Then
S = S & Args(N) & Sep
End If
On Error GoTo 0
End If
ContinueLoop:
Next N
'''''''''''''''''''''''''''''
' Remove the trailing Sep
'''''''''''''''''''''''''''''
If Len(Sep) > 0 Then
If Len(S) > 0 Then
S = Left(S, Len(S) - Len(Sep))
End If
End If
StringConcat = S
'''''''''''''''''''''''''''''
' Success. Get out.
'''''''''''''''''''''''''''''
Exit Function
ErrH:
'''''''''''''''''''''''''''''
' Error. Return #VALUE
'''''''''''''''''''''''''''''
StringConcat = CVErr(xlErrValue)
End Function
函数StringConcat(Sep作为字符串,ParamArray Args())作为变量
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
"金丝康卡特"
由奇普·皮尔森所作,chip@cpearson.com,www.cpearson.com
'www.cpearson.com/Excel/stringconcatenation.aspx
'此函数连接Args数组中的所有元素,
'由Sep字符分隔为单个字符串。此函数
'可以在数组公式中使用。有一个VBA强加的限制
'传入数组中的字符串(例如,从
'工作表单元格中的数组公式)必须少于256个字符。
'有关详细信息,请参阅字符串过长处的注释。
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
像线一样变暗
长
我和你一样长
调光范围
暗NumDims为长
暗磅一样长
Dim IsArrayAlloc作为布尔值
'''''''''''''''''''''''''''''''''''''''''''
'如果未传入任何参数,则返回
'vbNullString。
'''''''''''''''''''''''''''''''''''''''''''
如果UBound(Args)-LBound(Args)+1=0,则
StringConcat=vbNullString
退出功能
如果结束
对于N=LBound(Args)到UBound(Args)
''''''''''''''''''''''''''''''''''''''''''''''''
'在Args中循环
''''''''''''''''''''''''''''''''''''''''''''''''
如果IsObject(Args(N))=真,则
'''''''''''''''''''''''''''''''''''''
“反对
“如果我们有一个对象,请确保它
“这是一个范围。范围对象
'是我们将使用的唯一对象类型
“与……合作。还有别的原因吗
“a#值错误。
''''''''''''''''''''''''''''''''''''
如果参数的类型(N)是Excel.Range,则
'''''''''''''''''''''''''''''''''''''''''
'如果它是一个范围,则在
'单元格并创建附加元素
'到字符串S。
'''''''''''''''''''''''''''''''''''''''''
对于Args(N)单元格中的每个R
如果Len(R.Text)>0,则
S=S&R.文本和Sep
如果结束
下一个R
其他的
'''''''''''''''''''''''''''''''''
'不支持的对象类型。返回
“a#值错误。
'''''''''''''''''''''''''''''''''
StringConcat=CVErr(Xlerr值)
退出功能
如果结束
ElseIf IsArray(Args(N))=则为真
'''''''''''''''''''''''''''''''''''''
'阵列
'如果Args(N)是数组,请确保
'是一个已分配的数组。
'''''''''''''''''''''''''''''''''''''
IsArrayAlloc=(非IsError(LBound(Args(N))和_
(LBound(Args(N))0那么
如果Len(S)>0,则
S=左(S,透镜(S)-透镜(Sep))
如果结束
如果结束
StringConcat=S
'''''''''''''''''''''''''''''
“成功,滚出去。
'''''''''''''''''''''''''''''
退出功能
呃:
'''''''''''''''''''''''''''''
'错误。返回#值
'''''''''''''''''''''''''''''
StringConcat=CVErr(Xlerr值)
端函数
如果您使用的是最新的365或在线excel,则TEXTJOIN(“|”),TRUE,If($B$1:$B$5=“User1”,$A$1:$A$5,”)
。这是一个数组公式,所以请按ctrl-shift-enter键确认。否则,您需要对第2列进行排序,并使用辅助列将每一行与其下方的行连接起来。然后使用vlookup来获取第一行。如果排序不是您想要的,则vba是唯一的方法。以下是公式路由的示例。这有助于我找到答案。Th脚踝!!!