Hi there,
I've created some code which formats data into a table that can be used for a Pivot Table. However, there are c. 20,000 rows for the loop to go through and so far it has taken 50 mins to run and only completed 1,000 rows, and still hasn't finished. I was wondering if anyone would mind taking a look to see if there are any ways of speeding it up. Thanks!
I've created some code which formats data into a table that can be used for a Pivot Table. However, there are c. 20,000 rows for the loop to go through and so far it has taken 50 mins to run and only completed 1,000 rows, and still hasn't finished. I was wondering if anyone would mind taking a look to see if there are any ways of speeding it up. Thanks!
VBA Code:
Option Explicit
Sub SortHoldingData()
'This macro sorts all the holding data by security, asset class and portfolio
Application.ScreenUpdating = False
'First we clear the existing data
Sheets("Full Holding Data (2)").Select
Dim i As Integer
i = Cells(Rows.Count, 4).End(xlUp).Row
Range("A2:O" & i).Clear
'Next we move to the raw data exported from RID and define how many rows we are going to loop through
Sheets("Raw Holding Data").Select
Dim NumRows As Integer
Dim x As Integer
NumRows = Cells(Rows.Count, 2).End(xlUp).Row
'Here we are creating a loop to check if the row contains an ASSET CLASS, a SECURITY or a FUND REF. Depending on what comes up the data will be sorted and pasted into the appropriate column on the Full Holding Data tab
For x = 2 To NumRows
'This is checking to see if Column A is blank. If it is then the data in this row MUST be an asset class as it has no SEDOL or Fund Ref.
If Cells(x, 1) = "" Then
Range("B" & x).Copy
Sheets("Full Holding Data (2)").Select
Range("F" & x).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Raw Holding Data").Select
'We want to store the sector data.
Dim Sector As String
Sector = Range("B" & x).Value
'If the above is not true then the data in the row must be a security or a fund ref. Here we are checking to see if it a security by checking if there is anything in the rating column.
ElseIf Cells(x, 3) <> "" Then Range("B" & x).Copy
Sheets("Full Holding Data (2)").Range("D" & x).PasteSpecial Paste:=xlPasteValues
Sheets("Raw Holding Data").Select
Range("A" & x).Copy
Sheets("Full Holding Data (2)").Range("A" & x).PasteSpecial Paste:=xlPasteValues
Sheets("Raw Holding Data").Select
'We also want to store the security name.
Dim secName As String
secName = Range("B" & x).Value
'Finally if the above two statements are false then it must be a fund ref, so now we copy down.
Else: Range("A" & x).Copy
Sheets("Full Holding Data (2)").Range("B" & x).PasteSpecial Paste:=xlPasteValues
Sheets("Raw Holding Data").Range("B" & x).Copy
Sheets("Full Holding Data (2)").Range("C" & x).PasteSpecial Paste:=xlPasteValues
Sheets("Raw Holding Data").Range("G" & x).Copy
Sheets("Full Holding Data (2)").Range("K" & x).PasteSpecial Paste:=xlPasteValues
Sheets("Full Holding Data (2)").Range("D" & x).Value = secName
Sheets("Full Holding Data (2)").Range("F" & x).Value = Sector
End If
Next x
Application.ScreenUpdating = True
End Sub
Last edited by a moderator: