Hello
I'm once again troubling this forum's community with a puzzle that has been driving me crazy for the past 2 days.
I wrote a code that has to insert rows when certain conditions are met. The code finds the conditions, reads a number (X) in a row that meets those conditions and inserts X amount of rows. These has to go on for the entire sheet untill all numbers have been found and all the rows inserted.
My problem is that the code takes a reaaally long time to execute. The first time I tried it out in a real case scenario ( I had tested it in smaller samples of data) it ran for nearly half an hour before I thought it was stuck... so using the glamorous "esc" method I ended it. Using the debug option I found out, to my surprise, that the code wasn't stuck! It was working... really slowly but it was working. All variables had valid values and were increasing accordingly to the loop they were in. It was just taking a really long while. I ran it a few more times in smaller versions and came to the conclusion that the code does work, but it is slow.
I made several tests to see what part of the code was being the slow one, so by placing breakpoints and debug prints I found out that the insert function itself was the one that was being slow. I believe this is somehow related to the fact that whenever I insert a row, it has to displace ALL the rows below it to a different row.
I was wondering if it was possible to insert a range of rows at once, because in some cases my code has to insert over a thousand rows consecutively and it does it one by one. I think that if I can insert the 1k rows at once it would really improve my code's speed. Something like:
Insert.Range(X_amount_of_rows).EntireRow
Or somewhere among those lines... instead of inserting row by row untill the 1k rows are inserted and then jump to the next row that meets the condition. One thing that might be important to mention is that the files I am working with are really large. I have to process over 40k rows in each file and I estimate that the code would have to insert at least another 40k rows in each file... hence my need to "optimize" the code.
Here is a trimmed version of my code:
Any ideas?
Thanks a LOT for any help =D I am new to excel vba and I know my methods are really slow... so any help will be really appreciated!
Bear
I'm once again troubling this forum's community with a puzzle that has been driving me crazy for the past 2 days.
I wrote a code that has to insert rows when certain conditions are met. The code finds the conditions, reads a number (X) in a row that meets those conditions and inserts X amount of rows. These has to go on for the entire sheet untill all numbers have been found and all the rows inserted.
My problem is that the code takes a reaaally long time to execute. The first time I tried it out in a real case scenario ( I had tested it in smaller samples of data) it ran for nearly half an hour before I thought it was stuck... so using the glamorous "esc" method I ended it. Using the debug option I found out, to my surprise, that the code wasn't stuck! It was working... really slowly but it was working. All variables had valid values and were increasing accordingly to the loop they were in. It was just taking a really long while. I ran it a few more times in smaller versions and came to the conclusion that the code does work, but it is slow.
I made several tests to see what part of the code was being the slow one, so by placing breakpoints and debug prints I found out that the insert function itself was the one that was being slow. I believe this is somehow related to the fact that whenever I insert a row, it has to displace ALL the rows below it to a different row.
I was wondering if it was possible to insert a range of rows at once, because in some cases my code has to insert over a thousand rows consecutively and it does it one by one. I think that if I can insert the 1k rows at once it would really improve my code's speed. Something like:
Insert.Range(X_amount_of_rows).EntireRow
Or somewhere among those lines... instead of inserting row by row untill the 1k rows are inserted and then jump to the next row that meets the condition. One thing that might be important to mention is that the files I am working with are really large. I have to process over 40k rows in each file and I estimate that the code would have to insert at least another 40k rows in each file... hence my need to "optimize" the code.
Here is a trimmed version of my code:
Code:
Sub NewRow()
Application.ScreenUpdating = False
Dim X As Integer
Dim hCell As Range
Dim Y As Integer
Dim Already As Boolean
Dim aCounter As Integer
aCounter = 0
Dim LastRow5 As Integer
LastRow5 = Range("D4224:D45000").End(xlDown).Row
Debug.Print LastRow5
Already = False
For Each hCell In Range(Cells(4224, "DE"), Cells(LastRow5 + 40000, "DE"))
'hCell.Select
If Already = False Then
X = hCell.Value
X = X - 1
hCell.Select
If hCell.Value <> "" Then
For Y = 1 To X
hCell.Offset(1, 0).EntireRow.Insert
'LastRow5 = LastRow5 + 1
aCounter = 0
Next Y
If hCell.Offset(0, 1) <> "" Then
Already = True
End If
End If
ElseIf hCell.Value <> "" Then
Already = False
ElseIf hCell.Value = "" Then
aCounter = aCounter + 1
End If
If aCounter > 25 Then
Exit For
End If
Next hCell
Application.ScreenUpdating = True
End Sub
Any ideas?
Thanks a LOT for any help =D I am new to excel vba and I know my methods are really slow... so any help will be really appreciated!
Bear