+ Reply to Thread
Results 1 to 5 of 5

Macro stops too early - Help!

Hybrid View

  1. #1
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Macro stops too early - Help!

    Hi Everyone,

    I have a macro that inserts a new line under certain conditions (where "RAN" is shown)

    The problem I have is that it works perfectly for the first 46 rows but will not work thereafter.

    Can anyone help and advise where the issue is?

    Thanks

    Heres the code:-

    Option Explicit
    
    Sub new_inspection()
    Dim lrow As Long, i As Long
    
    With Worksheets("Sheet1")
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
        For i = 2 To lrow
            If .Range("W" & i).Value = "RAN" Then
                .Rows(i + 1).Insert
                .Range("A" & i & ":R" & i).Copy .Range("A" & i + 1)
                .Range("R" & i + 1).Value = "7G"
                .Range("W" & i + 1).Value = "RAN"
                i = i + 1
                lrow = lrow + 1000
            End If
        Next i
    End With
    
    End Sub
    Last edited by cossie2k; 06-25-2012 at 07:52 AM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro stops too early - Help!

    Hi cossie2k
    Look at this link, post #2 and pay particular attention to this comment
    The trick with this problem is to have the code start at the last row and work up so that the row re-numbering due to the row insertion does not mess up the macro operation.
    http://www.mrexcel.com/forum/showthr...-condition-met
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Macro stops too early - Help!

    Thanks John,

    Reading over the link you attached, I have two questions:-

    I though my original code (which has been doctored from this site!) was working from the end, up

        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
    The other thing is that Im not sure how to bend the formula on the link
    Sub InsertRows()
    Dim iRow As Long
    Dim rCount As Integer 'running count of inserted rows
    Dim iCount As Integer 'number of rows to insert
    Dim i As Integer
    Dim Bval As Variant 'Col B cell value to be written to new lines
    rCount = 0
    For iRow = [a65536].End(xlUp).Row To 2 Step -1
    If Cells(iRow, 5) = 1 Then
    iCount = Cells(iRow, 1) - Cells(iRow - 1, 1) - 1
    Bval = Cells(iRow, 2)
    For i = 1 To iCount
    rCount = rCount + 1
    Rows(iRow).Insert
    Cells(iRow, 2) = Bval
    Next i
    End If
    Next iRow
    Beep
    MsgBox rCount & " rows inserted", vbInformation, "Results"
    End Sub
    around what Im after. I dont understand what the component part are and how my requirements (based on my code) interact with these. I have tried

    Sub insertrows()
    
    Dim iRow As Long
    Dim rCount As Integer 'running count of inserted rows
    Dim iCount As Integer 'number of rows to insert
    Dim i As Integer
    Dim Bval As Variant 'Col B cell value to be written to new lines
    rCount = 0
    
    For iRow = [a65536].End(xlUp).Row To 2 Step -1
            If Cells(iRow, "W") = "RAN" Then
                iCount = Cells(iRow, 1) - Cells(iRow - 1, 1) - 1
                Bval = Cells(iRow, 2)
                For i = 1 To iCount
                rCount = rCount + 1
                Rows(iRow).Insert
                Cells(iRow, 2) = Bval
                
    Next i
    
            End If
    
        Next iRow
    
    End Sub
    But it fails at
    iCount = Cells(iRow, 1) - Cells(iRow - 1, 1) - 1
    Also how do I get the "7G" bit in?

    Thanks again
    Alex

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro stops too early - Help!

    Hi cossie2k

    Your original code was working down the rows. Step-1 is what causes the code to work from the bottom up. Glad you got it working.

  5. #5
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Macro stops too early - Help!

    Hi John,

    Ive fixed my original code.

    I went down to line 2000 (far more than I will be using) on entered data across that row. Dont know why its work but it has!

+ 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