+ Reply to Thread
Results 1 to 7 of 7

Macro keeps stopping in the same place when run all at once, but runs line-by-line

Hybrid View

  1. #1
    Registered User
    Join Date
    07-18-2014
    Location
    Oxnard, CA, USA
    MS-Off Ver
    2010
    Posts
    4

    Macro keeps stopping in the same place when run all at once, but runs line-by-line

    Hi all,

    I just started using VBA very recently and haven't had any official training. I've been using the loop below in order to move data from worksheets into a single form that can be printed out, and it's been working perfectly fine until just recently. I'm not sure if something I've done outside of the loop (the subroutine is rather large in order to make the form a one-click button) has somehow destabilized what's in here, and I would greatly appreciate any help.

    The "LastRow2 = Cells.Find(What:="Div. 10 SH & N Packinghouse - 1J", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row" statement is causing the macro to stop and move to debugging mode, highlighting the line, but not displaying an error. If I run the macro line by line in debugging mode, there are no errors and the macro runs normally.

    Excel 2010

    
        For Each ws2 In Worksheets
        Select Case UCase(ws2.Name)
            Case "LINE A", "LINE B", "LINE C", "LINE D", "LINE E", "LINE F", "LINE I", "LINE J", "LINE K"
                With ws2
                    
    
                '
                ' Copying and pasting the lines to the sanitation 'master schedule'
                '
                
                Sheets(ws2.Name).Activate
                LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                str1 = "A2:G" & LastRow
     
                Sheets(WSName).Activate
    
                LastRow2 = Cells.Find(What:="Div. 10 SH & N Packinghouse - 1J", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                
                long1 = LastRow + LastRow2 - 1
                str2 = LastRow2 & ":" & long1
                
                Rows(str2).Insert Shift:=xlDown
        
                str2 = "A" & LastRow2
                
                '
                ' Pasting the name of the section into the master schedule
                '
    
                str3 = ws2.Name & " Equipment"
     
                Range(str2) = str3
                
                str2 = "A" & LastRow2 & ":G" & LastRow2
                    With Range(str2)
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                    .MergeCells = True
                    End With
                    With Range(str2).Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .ThemeColor = xlThemeColorDark1
                    .TintAndShade = -0.14996795556505
                    End With
                    With Range(str2).Font
                    .Name = "Calibri"
                    .Size = 12
                    End With
                    
                LastRow2 = LastRow2 + 1
                str2 = "A" & LastRow2
                ThisWorkbook.Worksheets(ws2.Name).Range(str1).Copy Destination:=ThisWorkbook.Worksheets(WSName).Range(str2)
      
        
                End With
            End Select
        Next ws2

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,100

    Re: Macro keeps stopping in the same place when run all at once, but runs line-by-line

    Nothing to test it with ...
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    07-18-2014
    Location
    Oxnard, CA, USA
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro keeps stopping in the same place when run all at once, but runs line-by-line

    Sanitation and Machine ID Master List v2.xlsm

    I've attached a version of the workbook I'm working on. The workbook(v2) works some of the time, but other times it will halt on the aforementioned statement. It's really almost a coin toss whether or not it will work.

    I just listed the section initially in case there was something wrong with my syntax or some mistyping error.
    Attached Files Attached Files

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,100

    Re: Macro keeps stopping in the same place when run all at once, but runs line-by-line

    OK, let's assume I know absolutely nothing about your workbook, what it contains, what you are trying to do or how you are trying to do it.

    What is the process that you start that ends with a row of code highlighted? What sheet do you select? What data do you enter? What initiates the process that fails? Do you press a button, select a cell, change a drop down? Which subroutine is executed?

    What would you see if it worked? Are you getting some of the expected output? Or none of it? Does it update a worksheet? Or create a new sheet?


    Regards, TMS

  5. #5
    Registered User
    Join Date
    07-18-2014
    Location
    Oxnard, CA, USA
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro keeps stopping in the same place when run all at once, but runs line-by-line

    What the loop is doing with the 'problem' cells.find statement above is to go to sheets whose names are listed as Cases, e.g. Line A, Line B, Line C, etc.

    Whenever it goes to one of these worksheets (for example Line B), it determines what the last row in the A column is, then makes a string of the address of A2 to G(lastrow).

    It then goes to the WSName worksheet, the main worksheet where the form is being created, and determines what row the next section starts in order to insert a number of rows right above it, pushing the next section down, that will fit the future copy range.

    It then inserts the new sub-section name (Line B Equipment), copies the range from the Line B sheet, and pastes it in the WSName worksheet in the range it created by inserting rows.


    It then goes to the next case "Line C", and starts over again

  6. #6
    Registered User
    Join Date
    07-18-2014
    Location
    Oxnard, CA, USA
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro keeps stopping in the same place when run all at once, but runs line-by-line

    With this loop, somehow and SOMETIMES the cells.find statement (the one that is finding the Title of the next section) is making the macro enter debugging mode when the whole macro is run at once. Other times it works perfectly. The cell it's trying to find is always there (the title's name / cell.value never changes), so I'm not sure where exactly the error is coming from, especially since it's not happening all the time.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,100

    Re: Macro keeps stopping in the same place when run all at once, but runs line-by-line

    If it were me, I'd have a list of worksheets in a separate Config worksheet, in the order they need to be processed/attached to the report. Making it external to the code makes it easier to amend/update.

    I'd read that list into an array and then loop through it. You can then add an appropriate header at the bottom and copy and paste the rows following it. That saves having to keep working out how many rows there are and inserting them.

    But that's just me

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 4
    Last Post: 06-25-2014, 03:22 PM
  2. Macro runs line by line but not as one?
    By timwilks13 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2014, 09:01 AM
  3. Altering this macro: Insert copy of line in same place on 2 sheets!
    By gangel in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-16-2012, 04:10 AM
  4. Macro to move cells (Col B to Col V) to other sheet. Place line number in col A
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-29-2011, 03:43 PM
  5. Macro problem on, Yellowed line - previous line or next line.
    By Ed in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-29-2005, 07:06 PM

Tags for this Thread

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