Check value and insert row

undemane

Board Regular
Joined
Nov 19, 2007
Messages
75
I have data in a worksheet (say row1, row 2, row 3, etc.), and column D has a number in it. I want to check that number. If it is 0 to 6, do nothing; if it is between 6 & 12, I want to insert a blank row (row #2); if it is between 13 & 18, then add two rows (row2, row3), and so on. What formula should I use? Macro?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
'quote
'I have data in a worksheet (say row1, row 2, row 3,
'etc.), and column D has a number in it.
'I want to check that number. If it is 0 to 6,
'do nothing; if it is between 6 & 12,
'I want to insert a blank row (row #2);
'if it is between 13 & 18, then add
'two rows (row2, row3), and so on. What formula
'should I use? Macro?
'unquote

'SUPPOSE YOU WANT TO FIND THE VALUE 5 IN COLUMN D
' I PRESUME THAT 5 OCCURS ONLY ONCE IN COLUMN D
'CHANGE THE VALULE 5 TO YOUR REQUIREMNT . IF THIS IS STRING
'ENCLOSE IT WITHIN DOUBLE QUOTES
'POST FEEDBACK.

IMPORTANT: if you want to recheck the macro you have to first remove the blank rows and then run th macro

Code:
Sub test()
Dim k As Integer
Dim cfind As Range
With Columns("D:D")
Set cfind = .Cells.Find(what:=5, lookat:=xlWhole)
If cfind Is Nothing Then GoTo line1
'MsgBox cfind.Row
If cfind.Row <= 6 Then GoTo line1
k = cfind.Row / 6
'MsgBox k
Range(Cells(2, "a"), Cells(k + 1, "a")).EntireRow.Insert
GoTo line1
End With
line1:
End Sub
 
Last edited:
Upvote 0
I think this will do what you want
Code:
Sub test()
Dim insertCount As Long
Dim i As Long
With ThisWorkbook.Sheets("sheet1")
    For i = .Cells(.Rows.Count, 4).End(xlUp).Row To 1 Step -1
        With .Cells(i, 4)
            insertCount = Int((Val(CStr(.Value)) - 1) / 6)
            If 0 < insertCount Then
                .Offset(1, 0).Resize(insertCount, 1).EntireRow.Insert
            End If
        End With
    Next i
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,325
Messages
6,124,252
Members
449,149
Latest member
mwdbActuary

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