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:

Day/Night ShiftDate CodeValue
Day1/1/20180
Day1/1/20181
Night1/1/20180
Night1/1/20180
Night1/1/20180
Day1/2/20182
Night1/2/20180
Night1/2/20181
Day1/3/20183
Night1/3/20183

<tbody>
</tbody>


Desired output:

Day/Night ShiftDate CodeValue
Day1/1/20180
Day1/1/20181
Night1/1/20180
Night1/1/20180
Night1/1/20180
Day1/2/20182
Night1/2/20180
Night1/2/20181
Day1/3/20182
Night1/3/20182

<tbody>
</tbody>
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
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:

Day/Night ShiftDate CodeValue
Day1/1/20180
Day1/1/20181
Night1/1/20180
Night1/1/20180
Night1/1/20180
Day1/2/20182
Night1/2/20180
Night1/2/20181
Day1/3/20183
Night1/3/20183

<tbody>
</tbody>


Desired output:

Day/Night ShiftDate CodeValue
Day1/1/20180
Day1/1/20181
Night1/1/20180
Night1/1/20180
Night1/1/20180
Day1/2/20182
Night1/2/20180
Night1/2/20181
Day1/3/20182
Night1/3/20182

<tbody>
</tbody>
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,215,415
Messages
6,124,768
Members
449,187
Latest member
hermansoa

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