I have a macro that formats data for me. I've attached a spreadsheet with it in and the data it formats but for some reason it just keeps on running and won't stop once finished. I have to hit ESC twice to get out of it.
Dim lRow As Long
Dim lLastTime As Double
Dim iTimeCount, iLoopCounter As Integer
lLastTime = Cells(2, 3)
iTimeCount = 1
For lRow = 3 To 350
If Cells(lRow, 3) <> lLastTime Then
For iLoopCounter = iTimeCount To 2
Rows(lRow).Insert Shift:=xlDown
Cells(lRow, 3) = Cells(lRow - 1, 3)
lRow = lRow + 1
Next iLoopCounter
lLastTime = Cells(lRow, 3)
iTimeCount = 1
Else
iTimeCount = iTimeCount + 1
If iTimeCount > 3 Then
Rows(lRow).Delete Shift:=xlUp
lRow = lRow - 1
End If
End If
Next lRow
End Sub
The problem is with the last part of the macro where it starts "Dim lRow As Long" and I have put the line "For lRow = 3 To 350" in the macro which I thought should make it stop running that part at row 350 but it seems to want to carry on for infinity!
The problem is caused because you are deleting rows & when your row count gets to 65 you delete a row & reset the lRow counter back 1. This causes the lRow counter to be stuck at 65
When deleting rows in a loop it is best to step backwards
If you are pleased with a member's answer then use the Scales icon to rate it
If my reply has assisted or failed to assist you I welcome your Feedback.
If you want to understand by yourself, please copy the bellow code paste in to module.
Resize VBE Window to 1/3 of normal size.
press F8 key and watch the action was occur on Excel.
Range(Cells(1,1), Cells(1,1).end(xldown)) is the same action that you click cell A1 then press ctrl+Shift+Down Arrow key to select the range from the first cell contain data to the last cells contain data only.
You don't need t Select to copy data, I have posted lots of examples of copying.
You don't need to have Do....Loop around yout .Finds - have a look at the VBA Help on Find, the example is pretty good. This is probably the major problem!
Hope that helps.
RoyUK
--------
For Excel Tips & Solutions, free examples and tutorials why not check out my web site
I have no idea what you mean.
Can you explain or show me where the error is and how to fix it!?!?
I'm a bit of a novice with macros but the error is definately where I have pointed out.
First of all, I recommend you to use step running do as the follow
Excel Window, Press Alt+F8 Key to activate Macro Dialog Box
Click Macro then click Edit Button
Resize VBE Window to 1/3 of normal window
Press F8 Key and see the action
If you want to now the variable's value, move the mousepoint place over the variable see the value on screentip.
It's good that you attach the sample of file contain the macro with the question and the result that you want rather than post question only. Because see the sample of file will help many helpdesk to advice solution of your problem.
I Eliminate unnecessary code of your macro as bellow
Bookmarks