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

Estrella

New Member
Joined
Aug 3, 2012
Messages
17
columnA
aaa
columnB
1
bbb0
ccc0
ddd0
eee4
fgh2

<tbody>
</tbody>

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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,215,339
Messages
6,124,373
Members
449,155
Latest member
ravioli44

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