VBA code to insert rows based on value. Current code breaking on zero

davie_aus

New Member
Joined
Jul 5, 2018
Messages
6
Hey all,
Ive found some code on a forum elsewhere and tried to retrofit it solve a problem im facing with formatting.

I need to insert a certain amount of blank rows based on a value. I.E: If the value is 3, i need to insert 3 blank rows below this cell. The code ive found works nicely, but when it gets to 0 it doesnt work. Can anyone cast your eye over it and let me know if you find anything that may help.

Current Code:
Sub InsertRowsIf()
Dim lr As Long, R As Range, i As Long
lr = Range("C" & Rows.Count).End(xlUp).Row
Set R = Range("C1", "C" & lr)

Application.ScreenUpdating = False
For i = R.Rows.Count To 1 Step -1

If IsNumeric(R.Cells(i, 1).Value) And Not IsEmpty(R.Cells(i, 1)) Then
R.Cells(i, 1).Offset(1, 0).Resize(R.Cells(i, 1).Value).EntireRow.Insert

End If
Next
End Sub

Input:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Day/Night Shift[/TD]
[TD]Date Code[/TD]
[TD]Value[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/1/2018[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/2/2018[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/2/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/2/2018[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/3/2018[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/3/2018[/TD]
[TD]3[/TD]
[/TR]
</tbody>[/TABLE]


Desired output:

[TABLE="class: cms_table_grid"]
<tbody>[TR]
[TD]Day/Night Shift[/TD]
[TD]Date Code[/TD]
[TD]Value[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/1/2018[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/2/2018[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/2/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/2/2018[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/3/2018[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/3/2018[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #FAFAFA"][/TD]
[TD="bgcolor: #FAFAFA"][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi. Try after replacing
And Not IsEmpty(R.Cells(i, 1)) The
n

by this
And R.Cells(i, 1).Value > 0 Then

Don't forget to reset ScreenUpdating to True at the code end:
Application.ScreenUpdating = True
End Sub



 
Upvote 0
Hey all,
Ive found some code on a forum elsewhere and tried to retrofit it solve a problem im facing with formatting.

I need to insert a certain amount of blank rows based on a value. I.E: If the value is 3, i need to insert 3 blank rows below this cell. The code ive found works nicely, but when it gets to 0 it doesnt work. Can anyone cast your eye over it and let me know if you find anything that may help.

Current Code:


Input:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Day/Night Shift[/TD]
[TD]Date Code[/TD]
[TD]Value[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/1/2018[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/2/2018[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/2/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/2/2018[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/3/2018[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/3/2018[/TD]
[TD]3[/TD]
[/TR]
</tbody>[/TABLE]


Desired output:

[TABLE="class: cms_table_grid"]
<tbody>[TR]
[TD]Day/Night Shift[/TD]
[TD]Date Code[/TD]
[TD]Value[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/1/2018[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/1/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/2/2018[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/2/2018[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/2/2018[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Day[/TD]
[TD]1/3/2018[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Night[/TD]
[TD]1/3/2018[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #FAFAFA"][/TD]
[TD="bgcolor: #FAFAFA"][/TD]
[/TR]
</tbody>[/TABLE]
just replace your
Code:
IsNumeric(R.Cells(i, 1).Value)

to

Code:
R.Cells(i, 1).Value <> 0

and if you have non numeric values on the 3rd column just add
Code:
R.Cells(i, 1).Value <> 0

it should look like this:
Code:
R.Cells(i, 1).Value <> 0 And IsNumeric(R.Cells(i, 1).Value) And Not IsEmpty(R.Cells(i, 1))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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