To answer your question, the problem is that the range you are working on is
expanding as the FOR/NEXT loop progresses.
On the first pass when i = 1, if B2 is larger than B1, then you are
inserting a new Row i, (incidentally that should be Rows(i) not Rows("ii"),
Ranges like Range("B2") should be in quotes but numbers, as you did in
Range("B" & i) should not.)
If you select Row1 and the manually insert a new Row the Row will be
inserted *above* the existing Row1. So this is exactly what your code, [if
the Rows("ii") was corrected], would do. When i is indexed up to 2 it now
checks B2 which was B1 until you inserted a new row so it is carrying out
the same test with the same result!. You therefore end up inserting 10 new
rows above the existing data.
Changing your code line to:
Rows(i + 1).Insert Shift:=xlDown
would work on the first pass and a new Row 2 would be inserted, however,
when the second pass is made we are now comparing the newly inserted blank
Row with one with data in it and no matter how small the number is, the
blank cell above it will *always* be smaller. Another new Row 2 will be
inserted and we are back into the same position that we were in the previous
scenario.
You *could* add a line after the line:
Rows(i + 1).Insert Shift:=xlDown
to say:
i = i + 1
This would cause the referenced Row to jump past the inserted Row and get
over the above problem. However, changing the counting variable during the
loop is a VERY BAD idea and in any case will not do what you want. It would
work for the first 5 rows but by that time i would be reading 10, ( 5
indexed by Next and 5 increased by i = i + 1). The second set of 5 cells in
Column B would not be tested.
One way to go is to not use the counting variable as a Row reference as
Stefi did or, using as much as your original code as possible, something
like:
Sub Test()
Dim i As Integer
i = 1
Do Until Worksheets("sheet1").Range("B" & i).Value = ""
If Worksheets("sheet1").Range("B" & i).Value < _
Worksheets("sheet1").Range("B" & i + 1).Value Then
Rows(i + 1).Insert Shift:=xlDown
i = i + 1
End If
i = i + 1
Loop
End Sub
Which will add rows until it finds an empty cell. If there may be data in
the [original] Row 11 then you would have to change it to something like:
Sub Test2()
Dim i As Integer
Dim cCount As Integer
i = 1
cCount = 1
Do Until cCount = 10
If Worksheets("sheet1").Range("B" & i).Value < _
Worksheets("sheet1").Range("B" & i + 1).Value Then
Rows(i + 1).Insert Shift:=xlDown
i = i + 1
End If
i = i + 1
cCount = cCount + 1
Loop
End Sub
--
HTH
Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings
(e-mail address removed)
Replace @mailinator.com with @tiscali.co.uk
xavi garriga said:
I know my programming level is low and sure the mistake is a begginers
mistake...I want to insert a row if the number in row i+1 is bigger than
number than number in row i.
The code I've written is this:
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 1 To 10
If Worksheets("sheet1").Range("B" & i).Value <
Worksheets("sheet1").Range("B" & i + 1).Value Then
Rows("i:i").Insert Shift:=xlDown
End If
Next
End Sub
I can't find the mistake, can you help me????
Thanks!