VBA - Issues with code to insert row depending on cell value, then move down

Estrella

New Member
Joined
Aug 3, 2012
Messages
17
[TABLE="width: 69"]
<tbody>[TR]
[TD]columnA
aaa[/TD]
[TD="align: right"]columnB
1[/TD]
[/TR]
[TR]
[TD]bbb[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]ccc[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]ddd[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]eee[/TD]
[TD="align: right"]4[/TD]
[/TR]
[TR]
[TD]fgh[/TD]
[TD="align: right"]2[/TD]
[/TR]
</tbody>[/TABLE]

Hi there, am trying to write a code so rows are ( or not ) inserted depending on the value of the cell.
If cell value is 0 or 1, there shoulb be zero row inserted.
if cell value is 2 or greater, there should be 2 or more rows inserted.
Rows need to be inserted down.

Below is the code I have started to write but as I am new with VBA I am really struggling, does anybody knows how to write it properly? Many thanks

Sub jasmine()


Application.ScreenUpdating = False


Dim irow As Integer, lastrow As Integer

For irow = 1 To lastrow

If IsEmpty (Range("B" & irow))
Shift.xlDown
Else: ActiveCell.EntireRow.Resize(rowsize:=ActiveCell.Value).Insert Shift:=xlDown
End If


Columns("b").ClearContents


Application.ScreenUpdating = True


End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try:-
Code:
[COLOR=navy]Sub[/COLOR] MG11Aug16
[COLOR=navy]Dim[/COLOR] Lst [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Dim[/COLOR] n [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
Lst = Range("B" & Rows.count).End(xlUp).Row
[COLOR=navy]For[/COLOR] n = Lst To 1 [COLOR=navy]Step[/COLOR] -1
    [COLOR=navy]With[/COLOR] Range("B" & n)
        [COLOR=navy]If[/COLOR] .Value > 1 [COLOR=navy]Then[/COLOR]
          .EntireRow.Resize(.Value).Insert
        [COLOR=navy]End[/COLOR] If
  [COLOR=navy]End[/COLOR] With
[COLOR=navy]Next[/COLOR] n
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thank you so much Mick.
Works beautifully, but is there a way to insert rows down instead of up?
If that makes any sense.
So if B1 = 2, insert 2 rows so will have B1 = 2 and 2 blank lines in B2 and B3?
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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