blacksmoke
New Member
- Joined
- Jan 29, 2014
- Messages
- 7
Hello,
I am using the code to loop through a large table of data (~170000 lines). It is verrrry slow. About a minute per 1000 lines. How can I speed things up?
Thanks,
Chris
I am using the code to loop through a large table of data (~170000 lines). It is verrrry slow. About a minute per 1000 lines. How can I speed things up?
Thanks,
Chris
Code:
Sub Fixtable()
Dim lo As Excel.ListObject
Dim loRow As Excel.ListRow
Dim oldRow As Long
Dim newRow As Long
Dim i As Long
Dim j As Integer
Dim k As Integer
Application.ScreenUpdating = False
Dim rowCount As Long
Set lo = Worksheets("Raw Data").ListObjects("Table_ExternalData_1")
With lo
' Make type column in position 6
.ListColumns.Add 6
.HeaderRowRange(6) = "Type"
' Make count column in position 7
.ListColumns.Add 7
.HeaderRowRange(7) = "Count"
rowCount = .DataBodyRange.rows.Count
For i = rowCount To 0 Step -1
For j = 1 To 3
oldRow = i
newRow = i + j
Set loRow = .ListRows.Add(newRow)
For k = 1 To 5
.DataBodyRange(newRow, k).Value = .DataBodyRange(oldRow, k).Value
Next
Select Case j
Case 1
.DataBodyRange(newRow, 6).Value = "Direct"
.DataBodyRange(newRow, 7).Value = .DataBodyRange(oldRow, 8).Value
Case 2
.DataBodyRange(newRow, 6).Value = "Indirect"
.DataBodyRange(newRow, 7).Value = .DataBodyRange(oldRow, 10).Value + _
.DataBodyRange(oldRow, 11).Value + _
.DataBodyRange(oldRow, 12).Value + _
.DataBodyRange(oldRow, 13).Value
Case 3
.DataBodyRange(newRow, 6).Value = "Total"
.DataBodyRange(newRow, 7).Value = _
.DataBodyRange(newRow - 1, 7).Value + _
.DataBodyRange(newRow - 2, 7).Value
End Select
Next
.ListRows(oldRow).Delete
Next i
.ListColumns("DirHeadCount").Delete
.ListColumns("GenHeadCount").Delete
.ListColumns("AdmHeadCount").Delete
.ListColumns("QAQCHeadCount").Delete
.ListColumns("NCSOHeadCount").Delete
.ListColumns("HrsPer").Delete
.ListColumns("CommHeadCount").Delete
End With
Application.ScreenUpdating = True
End Sub