VBA: 多个匹配列基于虚拟排序
VBA: Multiple matching columns sorted based on a dummy
我在 excel 中有一个巨大的数据表,我需要对其进行排序。
我需要两列来匹配基于 Dummy 的列。
虚拟变量是 'C'(call) 或 'P'(put)。另外两列是 'expiration date' 和 'strike price'。
我需要对数据表进行排序,使其仅包含具有相同到期日期和行使价的 C 和 P。
我一直在尝试在没有任何帮助的情况下上网搜索。我真的不知道如何在 VBA.
中对其进行编程
非常感谢任何帮助。
谢谢。
我认为的解决方案:(我在添加一个新的列-column W
- 是空的- 将行标记为"Matched",操作后将被删除。)
标记双胞胎(匹配行)的第一个子项:
Private Sub FindDoublets()
Dim intRowC As Long
Dim intRowP As Long
Application.ScreenUpdating = False
Range("W1").EntireColumn.Insert
For intRowC = 2 To ActiveSheet.UsedRange.Rows.Count
If Cells(intRowC, 6).Value = "C" Then
For intRowP = 2 To ActiveSheet.UsedRange.Rows.Count
If Cells(intRowP, 6).Value = "P" Then
If Cells(intRowC, 4).Value = Cells(intRowP, 4).Value And Cells(intRowC, 7).Value = Cells(intRowP, 7).Value Then
Cells(intRowC, 23).Value = "Matched"
Cells(intRowP, 23).Value = "Matched"
End If
End If
Next
End If
Next
Application.ScreenUpdating = True
End Sub
要删除的第二个子项不匹配,因此未标记行:
Private Sub DeleteNotMatchedRows()
Dim intRow As Long
Application.ScreenUpdating = False
For intRow = ActiveSheet.UsedRange.Rows.Count To 2 Step -1
If Cells(intRow, 23).Value <> "Matched" Then
Rows(intRow).Delete shift:=xlShiftUp
End If
Next
Range("W1").EntireColumn.Delete
Application.ScreenUpdating = True
End Sub
在您的示例中 sheet 在 W 列中输入此公式:=IF(F2="C",TRUE,FALSE)
并将此公式输入 X 列中:=COUNTIFS(D:D,D2,G:G,G2,W:W,NOT(W2))
现在,当实际行具有相应的 C / P 行时,X 列中将有“1”,否则为“0”。只需要过滤 0-s 并删除行。
你也可以用宏做同样的事情,但它更复杂。
我在 excel 中有一个巨大的数据表,我需要对其进行排序。 我需要两列来匹配基于 Dummy 的列。 虚拟变量是 'C'(call) 或 'P'(put)。另外两列是 'expiration date' 和 'strike price'。 我需要对数据表进行排序,使其仅包含具有相同到期日期和行使价的 C 和 P。
我一直在尝试在没有任何帮助的情况下上网搜索。我真的不知道如何在 VBA.
中对其进行编程非常感谢任何帮助。
谢谢。
我认为的解决方案:(我在添加一个新的列-column W
- 是空的- 将行标记为"Matched",操作后将被删除。)
标记双胞胎(匹配行)的第一个子项:
Private Sub FindDoublets()
Dim intRowC As Long
Dim intRowP As Long
Application.ScreenUpdating = False
Range("W1").EntireColumn.Insert
For intRowC = 2 To ActiveSheet.UsedRange.Rows.Count
If Cells(intRowC, 6).Value = "C" Then
For intRowP = 2 To ActiveSheet.UsedRange.Rows.Count
If Cells(intRowP, 6).Value = "P" Then
If Cells(intRowC, 4).Value = Cells(intRowP, 4).Value And Cells(intRowC, 7).Value = Cells(intRowP, 7).Value Then
Cells(intRowC, 23).Value = "Matched"
Cells(intRowP, 23).Value = "Matched"
End If
End If
Next
End If
Next
Application.ScreenUpdating = True
End Sub
要删除的第二个子项不匹配,因此未标记行:
Private Sub DeleteNotMatchedRows()
Dim intRow As Long
Application.ScreenUpdating = False
For intRow = ActiveSheet.UsedRange.Rows.Count To 2 Step -1
If Cells(intRow, 23).Value <> "Matched" Then
Rows(intRow).Delete shift:=xlShiftUp
End If
Next
Range("W1").EntireColumn.Delete
Application.ScreenUpdating = True
End Sub
在您的示例中 sheet 在 W 列中输入此公式:=IF(F2="C",TRUE,FALSE)
并将此公式输入 X 列中:=COUNTIFS(D:D,D2,G:G,G2,W:W,NOT(W2))
现在,当实际行具有相应的 C / P 行时,X 列中将有“1”,否则为“0”。只需要过滤 0-s 并删除行。
你也可以用宏做同样的事情,但它更复杂。