我有一系列由 7 列和不同行数组成的矩阵。我想要矩阵第2 列中的公司名称,如果第 4 列中的相应数据是“CM”聚合到每个矩阵的一个单元格中(假设所有不同的矩阵为 B3:B98),不同名称之间有一个空格. 请参阅下图以了解矩阵的外观示例
最终结果是,如果 G 列中同一行的单元格是“CM”,则 E 列中的所有公司名称都将在 B3 中聚合,下一个矩阵从 B4 中的 M 列开始,依此类推。
我在让我的 if 语句识别单元格内容中的“CM”或使用 Join 语句聚合结果方面取得了零成功。任何帮助深表感谢。
编辑:目标是将特定证券的所有承销商汇总在一个单元格中,以便可以在表格的另一部分轻松搜索该单元格以查找特定承销商的存在。
正如您所看到的,下面的代码不起作用。我撞到了一堵墙,因为我无法区分包含“CM”的单元格和不包含“CM”的单元格。(我知道下面的代码不会在任何单元格中聚合结果,只会将结果复制到 B 列中,正如我所说,这是一项已停止的正在进行的工作。)
Dim Ws5 As Worksheet: Set Ws5 = Worksheets(5)
'turn off some Excel functionality so code runs faster
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
'Compiles the managers in the matrices into a column on the MgrMatrix sheet to be used
'for the entry sheet column of underwriters.
Dim CoL As Range: Set CoL = Ws5.Range("D3:K104")
Dim CeL As Range
For Each CeL In CoL.Columns(4)
If CeL.Text = "CM" Then
CeL.Offset(0, -5) = "CM"
Else
CeL.Offset(0, -5) = CeL.Offset(0, -2).Value
End If
Next
编辑:使用 urdearboy 的代码,我将其修改为按以下方式在同一张表上处理多个矩阵。这个版本没有他那样的技巧,因为这个版本依赖于包含相同列数且不超过 100 行的所有矩阵。
For i = 7 To 857 Step 9
For y = 3 To 100
If Cells(y, i) = "CM" Then
s = s & Cells(y, i).Offset(0, -1).Value & " "
End If
Next y
If s = "" Then
s = "Sole Lead"
End If
Cells(Rows.Count, 2).End(xlUp).Offset(1, 0) = Trim(s)
s = ""
Next i
将代码粘贴VBE
到其中Sheet 5
(或您想在其上运行的任何工作表)。
字符串 ,s
将在循环检查“CM”匹配的列时自行构建。
按原样,代码将在添加的每个新值之间添加逗号like, so, and, so,
,然后在显示最终字符串之前删除末尾的最后一个逗号like, so, and, so
Option Explicit
Sub TextCM()
Dim i As Long, s As String
For i = 3 To Range("G" & Rows.Count).End(xlUp).Row
If Range("G" & i) = "CM" Then
s = s & Range("E" & i).Value & ", " 'Remove & ", " if you do not want the comma + space
End If
Next i
Range("B2") = Left(s, Len(s) - 2) 'Change to Range("B2") = s to not delete last character in string
End Sub
您应该能够弄清楚如何将其扩展到多个表(矩阵?)没问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句