'根据Code编码字符串,在Zone区域的第1列中查找匹配项,如果值等于Code 添加到返回内容里,返回所有的符合项。SplitorRow 查找到的行分隔符,SplitorRow 查到数据列之间的分隔符
参数:
-
- Code :查询的内容
- Zone :查询的区域
- Splitor :行分隔符
- SplitorRow :列分隔符
Function CollectMatchInfo2(Code As range, Zone As range, Splitor As String, SplitorRow As String)
Dim columnCount As Long, rowsCount As Long
Dim resultStr As String, codeStr As String
columnCount = Zone.Columns.Count
rowsCount = Zone.Rows.Count
'参数判断
If Code.Cells.Count <> 1 Then
CollectMatchInfo2 = "#参数有误"
Exit Function
End If
If columnCount < 2 Then
CollectMatchInfo2 = "#参数有误"
Exit Function
End If
'默认分隔符为 ;
If Splitor = "" Then
Splitor = ";"
End If
'函数功能
codeStr = Trim(Code.Cells(1, 1))
With Zone
For i = 1 To rowsCount Step 1
If .Cells(i, 1) = "" Then
Exit For
End If
If Trim(.Cells(i, 1)) = codeStr Then
'第一个匹配项,不添加分隔符
If resultStr <> "" Then
resultStr = resultStr & Splitor & Chr(10)
End If
'循环添加后面的列
For j = 2 To columnCount Step 1
If .Cells(i, j) <> "" Then
If j = 2 Then
resultStr = resultStr & .Cells(i, j)
Else
resultStr = resultStr & SplitorRow & .Cells(i, j)
End If
End If
Next
End If
Next
End With
CollectMatchInfo2 = resultStr
End Function
参数:
-
- Code :查询的内容
- Zone :查询的区域
- Splitor :行分隔符
'根据Code编码字符串,在Zone区域的第1列中查找匹配项,如果值等于Code 添加到返回内容里,返回所有的符合项。SplitorRow 查找到的行分隔符,查到数据列之间的分隔符为空格
Function CollectMatchInfo(Code As range, Zone As range, Splitor As String)
CollectMatchInfo = CollectMatchInfo2(Code, Zone, Splitor, " ")
End Function