breynolds0431
Active Member
- Joined
- Feb 15, 2013
- Messages
- 303
- Office Version
- 365
- 2016
- Platform
- Windows
Hello. I have a loop that goes through around 200,000 rows and needs to transpose some results to a separate sheet, effectively making the data columnar. This seems to take quite a long time. All of the source data is in column A, so there aren't any nested loops where multiple columns need to be evaluated.
Here's what the below does (slowly).... Goes through column A's cells looking for one of two prefixes - either a "IST" or a "CLT". If either cells are found, the nested loop will start to look for the next occurrence of a cell beginning with "CLT". There are some minor differences in how the range is determined for each, which is why they are separate IF statements. However, each will take the range that's found and transpose the results to the next open row on the "results" tab. Any ideas on a more efficient method? I wouldn't think 200k rows would be this slow, but I'm thinking that it's all due to the transpose paste.
Here's what the below does (slowly).... Goes through column A's cells looking for one of two prefixes - either a "IST" or a "CLT". If either cells are found, the nested loop will start to look for the next occurrence of a cell beginning with "CLT". There are some minor differences in how the range is determined for each, which is why they are separate IF statements. However, each will take the range that's found and transpose the results to the next open row on the "results" tab. Any ideas on a more efficient method? I wouldn't think 200k rows would be this slow, but I'm thinking that it's all due to the transpose paste.
VBA Code:
Sub testsplit()
Dim nSht As Worksheet: Set nSht = ActiveSheet
Dim oWS As Worksheet: Set oWS = Sheets("results")
Dim c As Range, ce As Range
Dim iLR As Long: iLR = nSht.Cells(nSht.Rows.Count, 1).End(xlUp).row
Dim oLR As Long
Dim bRow As Long, eRow As Long
For Each c In nSht.Range("A1:A" & iLR).Cells
'Determines first row on oWS (results)
If oWS.Cells(1, 1) = "" Then
oLR = 1
Else
oLR = oWS.Cells(oWS.Rows.Count, 1).End(xlUp).row + 1
End If
If Left(c.Value, 3) = "IST" Then
'Set beginning row to current c row
bRow = c.row
'now find next row for range to copy, will take ending row -1 to not pick up actual CLT row
For Each ce In nSht.Range("A" & bRow & ":A" & iLR).Cells
If Left(ce.Value, 3) = "CLT" Then
eRow = ce.row - 1
With nSht
.Range(.Cells(bRow, 1), .Cells(eRow, 1)).Copy
oWS.Cells(oLR, 1).PasteSpecial Transpose:=True
End With
'next row found, so the ce for can exit
Exit For
End If
Next ce
End If
If Left(c.Value, 3) = "CLT" Then
'Set beginning row to current c row
bRow = c.row
'now find next row for range to copy
'note that bRow is added to 1 so c.row isn't considered next occurance
For Each ce In nSht.Range("A" & bRow + 1 & ":A" & iLR).Cells
If Left(ce.Value, 3) = "CLT" Then
eRow = ce.row
With nSht
.Range(.Cells(bRow, 1), .Cells(eRow, 1)).Copy
oWS.Cells(oLR, 1).PasteSpecial Transpose:=True
End With
'next row found, so the ce for can exit
Exit For
End If
Next ce
End If
Next c
End Sub