+ Reply to Thread
Results 1 to 8 of 8

Macro won't stop running!

  1. #1
    Registered User
    Join Date
    05-02-2007
    Posts
    17

    Macro won't stop running!

    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.

    Here is the macro:

    PHP Code: 
    Sub D_VO_Prep()
    '
    Data_Prep Macro
    ' Macro recorded 24/04/2007 by Lee
    '
        
    Columns("A:A").Select
        Selection
    .Copy
        Columns
    ("B:B").Select
        ActiveSheet
    .Paste
        Selection
    .Replace What:="* "Replacement:=""LookAt:=xlPart_
            SearchOrder
    :=xlByRowsMatchCase:=FalseSearchFormat:=False_
            ReplaceFormat
    :=False
        Columns
    ("A:A").Select
        Selection
    .Replace What:=" *"Replacement:=""LookAt:=xlPart_
            SearchOrder
    :=xlByRowsMatchCase:=FalseSearchFormat:=False_
            ReplaceFormat
    :=False
        Columns
    ("B:B").Select
        Application
    .CutCopyMode False
        Selection
    .NumberFormat "0.00"
        
    Columns("C:C").Select
        Selection
    .Delete Shift:=xlToLeft
        Columns
    ("D:J").Select
        Selection
    .Delete Shift:=xlToLeft
        Range
    ("A1:C600").Sort Key1:=Range("B1"), Order1:=xlAscendingKey2:=Range _
            
    ("A1"), Order2:=xlDescendingHeader:=xlGuessOrderCustom:=1MatchCase _
            
    :=FalseOrientation:=xlTopToBottomDataOption1:=xlSortNormal_
            DataOption2
    :=xlSortNormal
            Columns
    ("A:C").Select
        With Selection
            
    .HorizontalAlignment xlCenter
            
    .VerticalAlignment xlBottom
            
    .WrapText False
            
    .Orientation 0
            
    .AddIndent False
            
    .IndentLevel 0
            
    .ShrinkToFit False
            
    .ReadingOrder xlContext
            
    .MergeCells False
        End With
        
        With Range
    ("B1:B600")
            Do
                
    Set rngFind = .Find(What:="run"After:=.Cells(11), LookIn:=xlFormulas_
                                    LookAt
    :=xlPartSearchOrder:=xlByRowsSearchDirection:=xlNext_
                                    MatchCase
    :=FalseSearchFormat:=False)
                
                If 
    rngFind Is Nothing Then Exit Do
                    
                
    rngFind.EntireRow.Delete
            Loop
        End With
        
        With Range
    ("B1:B600")
            Do
                
    Set rngFind = .Find(What:=""After:=.Cells(11), LookIn:=xlFormulas_
                                    LookAt
    :=xlPartSearchOrder:=xlByRowsSearchDirection:=xlNext_
                                    MatchCase
    :=FalseSearchFormat:=False)
                
                If 
    rngFind Is Nothing Then Exit Do
                    
                
    rngFind.EntireRow.Delete
            Loop
        End With
        Columns
    ("A:A").Select
        Selection
    .Insert Shift:=xlRight
        Rows
    ("1:1").Select
        Selection
    .Insert Shift:=xlDown
        Range
    ("A1").Select
        ActiveCell
    .FormulaR1C1 "Runners"
        
    Range("B1").Select
        ActiveCell
    .FormulaR1C1 "Track"
        
    Range("C1").Select
        ActiveCell
    .FormulaR1C1 "Time"
        
    Range("D1").Select
        ActiveCell
    .FormulaR1C1 "Horse"
        
    Range("E1").Select
        ActiveCell
    .FormulaR1C1 "Rating"
        
    Range("E2").Select
        
            Range
    ("F2").Select
        Application
    .CutCopyMode False
        ActiveCell
    .FormulaR1C1 "=PROPER(RC[-4])"
        
    Range("F2").Select
        Selection
    .AutoFill Destination:=Range("F2:F350"), Type:=xlFillDefault
        Range
    ("F2:F350").Select
        ActiveWindow
    .ScrollRow 1
        Selection
    .Copy
        Range
    ("B2").Select
        Selection
    .PasteSpecial Paste:=xlPasteValuesOperation:=xlNoneSkipBlanks _
            
    :=FalseTranspose:=False
        Range
    ("F2").Select
        Application
    .CutCopyMode False
        ActiveCell
    .FormulaR1C1 "=PROPER(RC[-2])"
        
    Range("F2").Select
        Selection
    .AutoFill Destination:=Range("F2:F350"), Type:=xlFillDefault
        Range
    ("F2:F350").Select
        ActiveWindow
    .ScrollRow 1
        Selection
    .Copy
        Range
    ("D2").Select
        Selection
    .PasteSpecial Paste:=xlPasteValuesOperation:=xlNoneSkipBlanks _
            
    :=FalseTranspose:=False
        Columns
    ("F:F").Select
        Application
    .CutCopyMode False
        Selection
    .Delete Shift:=xlToLeft
        
        
    Dim lRow 
    As Long
      Dim lLastTime 
    As Double
      Dim iTimeCount
    iLoopCounter As Integer
        
      lLastTime 
    Cells(23)
      
    iTimeCount 1
      
      
    For lRow 3 To 350
        
    If Cells(lRow3) <> lLastTime Then
          
    For iLoopCounter iTimeCount To 2
            Rows
    (lRow).Insert Shift:=xlDown
            Cells
    (lRow3) = Cells(lRow 13)
            
    lRow lRow 1
          Next iLoopCounter
          lLastTime 
    Cells(lRow3)
          
    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!

    Can anyone point out my error?
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    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

    Please Login or Register  to view this content.
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    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.

  3. #3
    Registered User
    Join Date
    06-11-2007
    Location
    Nonthaburee Province, Thailand
    MS-Off Ver
    MS Office 2003 to MS Office 2010
    Posts
    82

    Thumbs up Eliminate Unneccessary code to run faster

    Dear Wizard007

    Having seen your code found that your code too long and contain unneccessary code made macro take a long time to run.

    May I give the trick about edit code.

    When you see selection follow with select, you can reduce code because Selection refer to the selected range.

    For example:
    When you see

    Range("A2:A4").Select
    Selection.Copy


    You can eliminate select and selection to one line . This made macro run faster.

    Please Login or Register  to view this content.
    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.

    Please Login or Register  to view this content.
    The other unneccessary code of set text alignment in cell.

    From you code

    Please Login or Register  to view this content.
    you can eliminate code (red line) from above to

    Please Login or Register  to view this content.

    Please Login or Register  to view this content.
    can be change to

    Please Login or Register  to view this content.
    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.

    Please Login or Register  to view this content.
    can be modify to:

    Please Login or Register  to view this content.
    Please remember that do action on cell without select cell make macro run faster

    Please try the edit your code then step running. If there is any error please do not hesitate post your query here.

    Hope it will be helpful information to you.

    Best reguard
    Last edited by nattasiray; 07-04-2007 at 10:08 AM.
    N. Yauvasuta
    Power User Excel.

  4. #4
    Registered User
    Join Date
    05-02-2007
    Posts
    17
    Hi Mudraker,

    Thanks for the ammendment.
    Unfortunately it didn't work it gives a completely different result.

    The result is in a workbook in the attached spreadsheet.

    It should show exactly 3 entries for each time!

    I'm lost on this one so any help would be greatly appreciated.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    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

    Free DataBaseForm example

  6. #6
    Registered User
    Join Date
    05-02-2007
    Posts
    17
    Hi Roy,

    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.

  7. #7
    Registered User
    Join Date
    06-11-2007
    Location
    Nonthaburee Province, Thailand
    MS-Off Ver
    MS Office 2003 to MS Office 2010
    Posts
    82

    Step Running will Help you

    Quote Originally Posted by wizard007
    Hi Roy,

    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

    Please Login or Register  to view this content.
    The Remain you should step running and debug.

    I have question that, do the records are equal the each time that you run macro?

    If not you must insert line that count the amount of records
    Last edited by nattasiray; 07-05-2007 at 12:34 AM.

  8. #8
    Registered User
    Join Date
    05-02-2007
    Posts
    17
    Hi,

    Thanks for the tips.
    I've tried your new macro but the last section of the code still keeps on running and running.

    I need to know how to make it stop!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1