Insert multiple rows depends on cell value is getting error

dharmadme

New Member
Joined
Mar 19, 2014
Messages
17
Hi Friends,

I am trying to add insert rows using macro.
L column fills with numbers. if L1 value is 5 then below need to insert 5 rows.
I tried below Macro.
But it getting Error.
Please solve this and let me know whats wrong in it


Sub InsertRowswork1()

Dim LastNumber As Long: LastNumber = ActiveSheet.Range("L" & Rows.Count).End(xlUp).Row
While LastNumber >= 2
If Not IsEmpty(ActiveSheet.Range("L" & LastNumber)) Then
Dim NewRows As Long: NewRows = ActiveSheet.Range("L" & LastNumber).Value
Dim InsertIndex As Long: InsertIndex = 1
While InsertIndex <= NewRows
ActiveSheet.Range("L" & LastNumber + 1).EntireRow.Insert
InsertIndex = InsertIndex + 1
Wend
End If
LastNumber = LastNumber - 1
Wend

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
If you are getting an error in code, you should always state the error, what line it is on and preferably give a small set of sample data that causes the error to occur.

I just ran your code on a smalls set of data I made up and got no error so I have nothing to go on to help you.
 
Upvote 0
Thanks for your response Peter

I got an error on highlighted row.
Hints says NewRows = 0 LastNumber = 106 which are not have any data
Then I found hidden values are there in my sheet. thats the reason Last number went to empty rows which are not containing any data
is that problem?
how can i remove hidden values in cells and change if any bugs in below code


Sub InsertRowswork1()

Dim LastNumber As Long: LastNumber = ActiveSheet.Range("L" & Rows.Count).End(xlUp).Row
While LastNumber >= 2
If Not IsEmpty(ActiveSheet.Range("L" & LastNumber)) Then
Dim NewRows As Long: NewRows = ActiveSheet.Range("L" & LastNumber).Value
Dim InsertIndex As Long: InsertIndex = 1
While InsertIndex <= NewRows
ActiveSheet.Range("L" & LastNumber + 1).EntireRow.Insert
InsertIndex = InsertIndex + 1
Wend
End If
LastNumber = LastNumber - 1
Wend

End Sub
 
Last edited:
Upvote 0
SOLVED: Insert multiple rows depends on cell value is getting error

Thanks for your support.

I have solved this myself using below code

Code:
Sub Autofilterwork1()

Dim MyCell, Rng As Range
Set Rng = Range("L:L")
For Each MyCell In Rng
    If MyCell Like "=" Then
  
Call Eraseemptycells
  
Else

Call InsertRowswork1

End If
Next

End Sub
Sub Eraseemptycells()
      Dim rng1 As Range

Set rng1 = Columns(12)
With rng1
    .AutoFilter Field:=12, Criteria1:="="
    With rng1.Offset
        .ClearContents
        .ClearComments
    End With
    With rng1.Offset(0, -1)
        .ClearContents
        .ClearComments
    End With
End With

Call InsertRowswork1

End Sub
Sub InsertRowswork1()

    Dim LastNumber As Long: LastNumber = ActiveSheet.Range("L" & Rows.Count).End(xlUp).Row
    While LastNumber >= 2
        If Not IsEmpty(ActiveSheet.Range("L" & LastNumber)) Then
            Dim NewRows As Long: NewRows = ActiveSheet.Range("L" & LastNumber).Value
            Dim InsertIndex As Long:    InsertIndex = 1
            While InsertIndex <= NewRows
                ActiveSheet.Range("L" & LastNumber + 1).EntireRow.Insert
                InsertIndex = InsertIndex + 1
            Wend
        End If
        LastNumber = LastNumber - 1
    Wend

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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