For Next loop stops at row 13840

MkDr100

New Member
Joined
Nov 18, 2003
Messages
14
I have a vba that compares two columns of numbers. It loops through starting at row six and ending with the last row (in this case row 13867). It compares the values in each row and inserts rows if needed to align the numbers. This part works fine. The problem is that the For Next loop exits before it gets to the end, at row 13840. Even though my lastrow variable equals 13867. Any ideas on how to correct this?? I've run the macro on Excel 2002 and 2003 and I get the same results.

Thanks.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi there,

We'd need to see the code in order to make any kind of guess as to why you are getting the results you are. Care to post?
 
Upvote 0
<font face=Courier New><SPAN style="color:#007F00">'</SPAN>
<SPAN style="color:#007F00">' DECLARATION OF VARIABLES</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> strLastDay <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>                <SPAN style="color:#007F00">'last day of previous month</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> strLastRow1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>              <SPAN style="color:#007F00">'last row row number</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> strLastRow2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>              <SPAN style="color:#007F00">'last row row number</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> h <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    
    
    
    Sheets("Sheet 2").Activate
    Range("B6").Select
    <SPAN style="color:#007F00">'user to imput the last day of previous month to delete records</SPAN>
    strLastDay = InputBox("Enter the Last Day of the Previous Month" & _
        "(mmddyy format), then press OK or ENTER", "Last day of the previous month", _
        "103104")

    <SPAN style="color:#007F00">'delete last months records</SPAN>
    strLastRow1 = Range("B65536").End(xlUp).Row
    <SPAN style="color:#00007F">For</SPAN> h = strLastRow1 <SPAN style="color:#00007F">To</SPAN> 6 <SPAN style="color:#00007F">Step</SPAN> -1
        <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(h, 3) = strLastDay <SPAN style="color:#00007F">Then</SPAN>
            Range("B" & h, "F" & h).Select
            Selection.Delete Shift:=xlUp
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> h

    
    strLastRow1 = Range("B65536").End(xlUp).Row
    Range("B6").Select
    <SPAN style="color:#007F00">'Comparison Loop</SPAN>
    <SPAN style="color:#00007F">For</SPAN> i = 6 <SPAN style="color:#00007F">To</SPAN> strLastRow1
        <SPAN style="color:#00007F">If</SPAN> Cells(i, "E") = Cells(i, "I") <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">If</SPAN> Cells(i, "E") = Cells(i + 1, "I") <SPAN style="color:#00007F">Then</SPAN>
            Range("B" & i, "F" & i).Select
            Selection.Insert Shift:=xlDown
            <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(i + 1, "E") = Cells(i, "I") <SPAN style="color:#00007F">Then</SPAN>
            Range("G" & i, "J" & i).Select
            Selection.Insert Shift:=xlDown
            <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(i, "E") = Cells(i + 2, "I") <SPAN style="color:#00007F">Then</SPAN>
            Range("B" & i, "F" & i).Select
            Selection.Insert Shift:=xlDown
            Selection.Insert Shift:=xlDown
            <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(i + 2, "E") = Cells(i, "I") <SPAN style="color:#00007F">Then</SPAN>
            Range("G" & i, "J" & i).Select
            Selection.Insert Shift:=xlDown
            Selection.Insert Shift:=xlDown
            <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
Skip:
            strLastRow1 = Range("B65536").End(xlUp).Row
    <SPAN style="color:#00007F">Next</SPAN> i
    
    <SPAN style="color:#007F00">'Add Variance Column</SPAN>
    strLastRow2 = Range("G65536").End(xlUp).Row
    Range("K6").Select
    ActiveCell.FormulaR1C1 = "=RC[-6]-RC[-2]"
    ActiveCell.Copy
    Range("K6:K" & strLastRow2).Select
    ActiveSheet.Paste
    Application.CutCopyMode = <SPAN style="color:#00007F">False</SPAN>
    Range("B6").Select

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

I hope I did this right.
 
Upvote 0
Hi,

As stated above we really need to see the code. However, if you are inserting rows it sounds as though you may be setting the variable row number then inserting rows. Perhaps you need to work backwards from your max row (13867 in this case) and work back to row 1. This means that inserting rows doesn't impact on the number of rows left to trawl thruogh.

Not sure if that makes sense?

James
 
Upvote 0
Let me guess, you're inserting around 27 rows? Am I close?

When inserting or deleting, it's best to loop from the bottom up. You are affecting the very numbers which are aiding your loop ( i ). Try this as your loop start ...


<font face=Tahoma New>    <SPAN style="color:#007F00">'Comparison Loop</SPAN>
    <SPAN style="color:#00007F">For</SPAN> i = strLastRow1 <SPAN style="color:#00007F">To</SPAN> 6 <SPAN style="color:#00007F">Step</SPAN> -1</FONT>


.. see if that helps. You could shorten your whole code slightly, so the entire procedure might look like this ...

<font face=Tahoma New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> foofoo()
    <SPAN style="color:#007F00">'</SPAN>
    <SPAN style="color:#007F00">' DECLARATION OF VARIABLES</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> strLastDay <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>             <SPAN style="color:#007F00">'last day of previous month</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> strLastRow1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>              <SPAN style="color:#007F00">'last row row number</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> strLastRow2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>              <SPAN style="color:#007F00">'last row row number</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> h <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

    Sheets("Sheet 2").Activate
    Range("B6").Select
    <SPAN style="color:#007F00">'user to imput the last day of previous month to delete records</SPAN>
    strLastDay = InputBox("Enter the Last Day of the Previous Month" & _
    "(mmddyy format), then press OK or ENTER", "Last day of the previous month", _
    "103104")
    strLastRow1 = Range("B65536").End(xlUp).Row
    <SPAN style="color:#00007F">For</SPAN> h = strLastRow1 <SPAN style="color:#00007F">To</SPAN> 6 <SPAN style="color:#00007F">Step</SPAN> -1
        <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(h, 3) = strLastDay <SPAN style="color:#00007F">Then</SPAN>
            Range("B" & h, "F" & h).Delete
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> h
    strLastRow1 = Range("B65536").End(xlUp).Row
    <SPAN style="color:#00007F">For</SPAN> i = strLastRow1 <SPAN style="color:#00007F">To</SPAN> 6 <SPAN style="color:#00007F">Step</SPAN> -1
        <SPAN style="color:#00007F">If</SPAN> Cells(i, "E") = Cells(i, "I") <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">If</SPAN> Cells(i, "E") = Cells(i + 1, "I") <SPAN style="color:#00007F">Then</SPAN>
            Range("B" & i, "F" & i).Insert
            <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(i + 1, "E") = Cells(i, "I") <SPAN style="color:#00007F">Then</SPAN>
            Range("G" & i, "J" & i).Insert
            <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(i, "E") = Cells(i + 2, "I") <SPAN style="color:#00007F">Then</SPAN>
            Range("B" & i, "F" & i + 1).Insert
            <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(i + 2, "E") = Cells(i, "I") <SPAN style="color:#00007F">Then</SPAN>
            Range("G" & i, "J" & i + 1).Insert
            <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
Skip:
    <SPAN style="color:#00007F">Next</SPAN> i
    strLastRow2 = Range("G65536").End(xlUp).Row
    Range("K6:K" & strLastRow2).FormulaR1C1 = "=RC[-6]-RC[-2]"
    Range("B6").Select
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Yeah, 27 rows are added. How'd you know that???

Unfortunately, starting from the bottom won't work because the first set of data (after deleting out prior month records) is still over 100 records longer than the second set of data.
What I tried, since there are so many prior month records being deleted, was to NOT reset the strLastRow after the prior month records are deleted. This worked with this data set, but I don't know if the next data set will delete more prior period records than it adds. Here is the modified code. I've remarked out two lines of code.

Any idea why it stoped at 13840? Thanks again for all your suggestions.

<font face=Courier New><SPAN style="color:#007F00">' DECLARATION OF VARIABLES</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> strLastDay <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>                <SPAN style="color:#007F00">'last day of previous month</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> strLastRow1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>              <SPAN style="color:#007F00">'last row row number</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> strLastRow2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>              <SPAN style="color:#007F00">'last row row number</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> h <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    
    
    
    Sheets("Sheet 2").Activate
    Range("B6").Select
    <SPAN style="color:#007F00">'user to imput the last day of previous month to delete records</SPAN>
    strLastDay = InputBox("Enter the Last Day of the Previous Month" & _
        "(mmddyy format), then press OK or ENTER", "Last day of the previous month", _
        "103104")

    <SPAN style="color:#007F00">'delete last months records</SPAN>
    strLastRow1 = Range("B65536").End(xlUp).Row
    <SPAN style="color:#00007F">For</SPAN> h = strLastRow1 <SPAN style="color:#00007F">To</SPAN> 6 <SPAN style="color:#00007F">Step</SPAN> -1
        <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(h, 3) = strLastDay <SPAN style="color:#00007F">Then</SPAN>
            Range("B" & h, "F" & h).Select
            Selection.Delete Shift:=xlUp
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> h

    
    <SPAN style="color:#007F00">'***  strLastRow1 = Range("B65536").End(xlUp).Row</SPAN>
    Range("B6").Select
    <SPAN style="color:#007F00">'Comparison Loop</SPAN>
    <SPAN style="color:#00007F">For</SPAN> i = 6 <SPAN style="color:#00007F">To</SPAN> strLastRow1
        <SPAN style="color:#00007F">If</SPAN> Cells(i, "E") = Cells(i, "I") <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">If</SPAN> Cells(i, "E") = Cells(i + 1, "I") <SPAN style="color:#00007F">Then</SPAN>
            Range("B" & i, "F" & i).Select
            Selection.Insert Shift:=xlDown
            <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(i + 1, "E") = Cells(i, "I") <SPAN style="color:#00007F">Then</SPAN>
            Range("G" & i, "J" & i).Select
            Selection.Insert Shift:=xlDown
            <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(i, "E") = Cells(i + 2, "I") <SPAN style="color:#00007F">Then</SPAN>
            Range("B" & i, "F" & i).Select
            Selection.Insert Shift:=xlDown
            Selection.Insert Shift:=xlDown
            <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(i + 2, "E") = Cells(i, "I") <SPAN style="color:#00007F">Then</SPAN>
            Range("G" & i, "J" & i).Select
            Selection.Insert Shift:=xlDown
            Selection.Insert Shift:=xlDown
            <SPAN style="color:#00007F">GoTo</SPAN> Skip
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
Skip:
            <SPAN style="color:#007F00">'***  strLastRow1 = Range("B65536").End(xlUp).Row</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> i
    
    <SPAN style="color:#007F00">'Add Variance Column</SPAN>
    strLastRow2 = Range("G65536").End(xlUp).Row
    Range("K6").Select
    ActiveCell.FormulaR1C1 = "=RC[-6]-RC[-2]"
    ActiveCell.Copy
    Range("K6:K" & strLastRow2).Select
    ActiveSheet.Paste
    Application.CutCopyMode = <SPAN style="color:#00007F">False</SPAN>
    Range("B6").Select

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0
=13867-13840

..

=27

;)

It is off by the number of rows you insert. You NEED to come from the bottom up. If not, you are fighting a losing battle. Without seeing the spreadsheet, it is very hard for me (I'm a very visual person). Work on dong it like I said, from the bottom up, don't reset your number with every iteration. Maybe think of doing away with those current variables and just using the range row in the "For i =" line.
 
Upvote 0

Forum statistics

Threads
1,206,947
Messages
6,075,802
Members
446,158
Latest member
octagonalowl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top