I have this, and I think it's working? It's been running nearly all day and still isn't finished, so I'm hoping there is a better way to code it.
Right now I have a series of loops that iterate through all my data, but it's taking entirely too long. In short, I have three large worksheets, one master with one identifier, and two others with three possible identifiers.
SSB is the master sheet, containing CUSIP. EDM and BB are the data sheets, which may or may not contain CUSIP, ISIN, and SEDOL.
My goal is to create a new sheet, Identifiers, that compiles all of this data in one sheet. Unfortunately, the only way I know how to do this is loops within loops, these files are 5-30k rows each. I've tried to optimize the code for what it is, but at this point I'm hoping there's just a better method that what I've done. I've been running this all afternoon on a second computer and it's still not done.
As requested, the plain English version of what's going on:
- Take Column A from SSB as main identifier, CUSIP.
- Iterate through EDM column I for a match. If found, copy line to report worksheet, Identifiers.
- If not found, look through BB column A for a match.
- If a match is found here, check columns D and E, ISIN & SEDOL, against EDM columns G and H. If match is found copy line to Identifiers.
- If no match of any identifier on any worksheet, copy CUSIP to no identifiers worksheet.
- Once the list is parsed, check the identifiers worksheet CUSIP, ISIN, and SEDOL against BB and EDM to make sure no identifiers are missing.
Here is what I have:
Sub Identifiers()
Dim SSB As Worksheet
Dim EDM As Worksheet
Dim BB As Worksheet
Dim Identifiers As Worksheet
Dim NoIdentifiers As Worksheet
Dim lastrow As Long
Dim endSSB As Long
Dim endIdentifiers As Long
Dim endNoIdentifiers As Long
Dim CUSIP As Variant
Dim SEDOL As Variant
Dim ISIN As Variant
Dim BBlast As Variant
Dim EDMtable As Variant
Set SSB = Sheets("SSB Holdings")
Set EDM = Sheets("EDM Data")
Set BB = Sheets("BB Data")
Set Identifiers = Sheets("Identifiers")
Set NoIdentifiers = Sheets("No Identifiers")
'text to values
With EDM.UsedRange
.Value = .Value
End With
With BB.UsedRange
.Value = .Value
End With
'Clear prior data
Identifiers.Cells.ClearContents
NoIdentifiers.Cells.ClearContents
'Add headers to sheets
EDM.Range("A1:BP1").Copy
Identifiers.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
SSB.Range("A1:B1").Copy
NoIdentifiers.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'compile identifiers
i = 0
lastrow = EDM.Range("A" & Rows.Count).End(xlUp).Row
BBlast = BB.Range("A" & BB.Range("A" & Rows.Count).End(xlUp).Row)
EDMtable = EDM.Range("A1:I" & EDM.Range("A" & Rows.Count).End(xlUp).Row)
'Remove lines with no identifiers
For i = 2 To lastrow
If IsEmpty(EDM.Range("G" & i)) Then
If IsEmpty(EDM.Range("H" & i)) Then
If IsEmpty(EDM.Range("I" & i)) Then
Range("A" & i).EntireRow.Delete
End If
End If
End If
Next i
'Filter SSB CUSIPs
endSSB = SSB.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To endSSB
CUSIP = SSB.Range("A" & i)
For x = 2 To lastrow
If EDM.Range("I" & x).Value = CUSIP Then
endIdentifiers = Identifiers.Range("A" & Rows.Count).End(xlUp).Row
EDM.Range("I" & x).EntireRow.Copy
Identifiers.Range("A" & endIdentifiers + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ElseIf EDM.Range("I" & x).Value <> CUSIP Then
For y = 2 To BB.Range("A" & Rows.Count).End(xlUp).Row
If BB.Range("A" & y) = CUSIP Then
ISIN = BB.Range("D" & y)
SEDOL = BB.Range("E" & y)
For p = 2 To lastrow
If EDM.Range("G" & p).Value = ISIN Then
endIdentifiers = Identifiers.Range("A" & Rows.Count).End(xlUp).Row
EDM.Range("G" & p).EntireRow.Copy
Identifiers.Range("A" & endIdentifiers + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ElseIf EDM.Range("H" & p).Value = SEDOL Then
endIdentifiers = Identifiers.Range("A" & Rows.Count).End(xlUp).Row
EDM.Range("G" & p).EntireRow.Copy
Identifiers.Range("A" & endIdentifiers + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End If
Next p
End If
Next y
Else: SSB.Range("A" & i).EntireRow.Copy
NoIdentifiers.Range("A" & endIdentifiers + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End If
Next x
Next i
'Fill in CUSIP/ISIN/SEDOL in identifiers tab
endIdentifiers = Identifiers.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To endIdentifiers
If IsEmpty(Identifiers.Range("A" & i)) Then
ISIN = Identifiers.Range("G" & i)
SEDOL = Identifiers.Range("H" & i)
For x = 2 To BBlast
If BB.Range("D" & x) = ISIN Then
Identifiers.Range("A" & i).Value = BB.Range("A" & x)
If IsEmpty(Identifiers.Range("A" & i)) Then
For p = 2 To BBlast
If BB.Range("E" & p) = SEDOL Then
Identifiers.Range("A" & i).Value = BB.Range("A" & p)
End If
Next p
End If
End If
Next x
End If
Next i
endIdentifiers = Identifiers.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To endIdentifiers
If IsEmpty(Identifiers.Range("G" & i)) Then
CUSIP = Identifiers.Range("A" & i)
SEDOL = Identifiers.Range("H" & i)
For x = 2 To BBlast
If BB.Range("A" & x) = CUSIP Then
Identifiers.Range("G" & i).Value = BB.Range("D" & x)
If IsEmpty(Identifiers.Range("G" & i)) Then
For p = 2 To BBlast
If BB.Range("E" & p) = SEDOL Then
Identifiers.Range("G" & i) = BB.Range("D" & p)
End If
Next p
End If
End If
Next x
End If
Next i
endIdentifiers = Identifiers.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To endIdentifiers
If IsEmpty(Identifiers.Range("H" & i)) Then
CUSIP = Identifiers.Range("A" & i)
ISIN = Identifiers.Range("G" & i)
For x = 2 To BBlast
If BB.Range("A" & x) = CUSIP Then
Identifiers.Range("H" & i).Value = BB.Range("E" & x)
If IsEmpty(Identifiers.Range("H" & i)) Then
For p = 2 To BBlast
If BB.Range("D" & p) = ISIN Then
Identifiers.Range("H" & i) = BB.Range("E" & p)
End If
Next p
End If
End If
Next x
End If
Next i
End Sub