【发布时间】:2019-10-21 23:29:56
【问题描述】:
问题是第一列有重复值(金融产品的ISIN编号),但其他列有不同的特征(即不同的产品名称,不同的修改持续时间等)哪里应该是相同的特征。
我想查找我的第一列中已经存在的 ISIN 编号(至少两次),然后从其他列(发现重复值的同一行)中获取特定元素,例如发行人名称、修改的持续时间等并将它们粘贴到其他的 ISIN 元素中,以便在 ISIN 编号相同的情况下报告相同的元素(其他列中的数据)。 我还想比较这些重复产品的修改持续时间并取较大的一个(出于保守原因,因为这些数据用于进一步计算)。
Sub dup_cp()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Sheets("Investment Assets").Activate
j = Application.CountA(Range("A:A"))
'counts the number of filled in rows
For i = 5 To j
'it starts from line 5 on purpose, the ISIN numbers start from that line
For k = i + 1 To j
If Sheets("Investment Assets").Range(Cells(k, 55), Cells(k, 55)).Value = "Duplicate Value" Then GoTo skip_dup
'it skips the line that has already been detected as duplicated
If Sheets("Investment Assets").Range(Cells(k, 1), Cells(k, 1)).Value = Sheets("Investment Assets").Range(Cells(i, 1), Cells(i, 1)).Value Then
'it finds the duplicate value (ISIN number) in the first column
If Sheets("Investment Assets").Range(Cells(k, 29), Cells(k, 29)).Value >= Sheets("Investment Assets").Range(Cells(i, 29), Cells(i, 29)).Value Then
'it compares the 29th column values (the modified duration of the components) and keeps the bigger value for prudency reasons
Sheets("Investment Assets").Range(Cells(k, 15), Cells(k, 32)).Copy
Sheets("Investment Assets").Range(Cells(i, 15), Cells(i, 32)).PasteSpecial Paste:=xlPasteValues
Else
Sheets("Investment Assets").Range(Cells(i, 15), Cells(i, 32)).Copy
Sheets("Investment Assets").Range(Cells(k, 15), Cells(k, 32)).PasteSpecial Paste:=xlPasteValues
End If
Sheets("Investment Assets").Range(Cells(k, 55), Cells(k, 55)).Value = "Duplicate Value"
'it shows in the 55th column if the ISIN number is duplicated or not
Sheets("Investment Assets").Range(Cells(i, 55), Cells(i, 55)).Value = "Duplicate Value"
Else
Sheets("Investment Assets").Range(Cells(k, 55), Cells(k, 55)).Value = "-"
End If
skip_dup:
Next
Next
End Sub
此代码有效,但有点混乱,对此我深表歉意。 提前感谢所有愿意花时间让它更简单、更快捷的人。 我认为这将对在 Solvecy II 环境中工作的任何精算师或风险经理有所帮助。
【问题讨论】:
-
Copy是问题所在。它很慢。此外,应避免使用Activate,但您需要始终告诉 VBA 您指的是哪个工作表。 -
两者都同意。我可以使用而不是只复制等式。我使用了激活,因为之前还有其他代码使用其他工作表运行。
标签: excel vba duplicates copy copy-paste