Help with if loop statement

kevinh2320

Board Regular
Joined
May 13, 2016
Messages
61
I need help with an if/loop statement. I have data in my spreadsheet all of which is in column A. What I need to do is loop through all rows in column A searching for cells that contain the value of 50. If the cell below is more or less than 50 than I need to insert a row and set that cells value to 0. I need to loop through all cells in column A until the end repeating this action until complete.

Below is an example of the data that could be in my spreadsheet. In this particular example there is only one occurrence (highlighted in red) where I'd need a row inserted below that value of 50 and the new cell set to the value of 0. Appreciate any help!

1
23
2016
2
15
2016
196
176
50
2
20
2016
3
15
2016
196
196
50
50
3
19
2016
4
15
2016
196
196
50
50
4
30
2016
5
15
2016
196
196
50
50
5
28
2016
6
15
2016
196
196
50
50
6
25
2016
7
15
2016
196
196
50
50
7
23
2016
8
15
2016
196
196
50
50
8
20
2016
9
15
2016
196
196
50
50

<tbody>
</tbody>
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
What I need to do is loop through all rows in column A searching for cells that contain the value of 50.
If the cell below is more or less than 50 than I need to insert a row

196
176
50
2
196
50
50
3
19


<tbody>

</tbody>

In the same example you have 50 and below there is a 3, in that case you should not insert a row?
 
Upvote 0
How about
Code:
Sub kevinh2320()
   Dim i As Long
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
      If Cells(i, 1) = 50 Then
         If Cells(i - 1, 1) <> 50 Then
            Rows(i + 1).Insert
            Cells(i + 1, 1) = 0
         Else
            i = i - 1
         End If
      End If
   Next i
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
I guess it's a "no" to my question, haha.
I add my solution.
It is a bit longer, but it is certainly faster.

Code:
Sub Loop_statement()
  Dim f As Range, cell As String
  Set f = Range("A:A").Find(50, , xlValues, xlWhole, , xlPrevious)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      If f.Offset(-1) <> 50 And f.Offset(1) <> 50 Then
        f.Offset(1).EntireRow.Insert
        f.Offset(1) = 0
      End If
      Set f = Range("A:A").FindPrevious(f)
    Loop While Not f Is Nothing And f.Address <> cell
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,582
Members
449,089
Latest member
Motoracer88

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