0

Below is the piece of code in VBA which basically insert no of rows based on a count present in a specific cell, Now I want to change the code so that no of rows that will be inserted is to be one less then the count present in a specific cell. for eg- if in a specific column and specific cell count=N then macro will runn and add N no of rows.Now I want rows is to be added is one less i.e N-1

Sub InsertRowsIf()
Dim lr As Long, R As Range, i As Long
lr = Range("R" & Rows.Count).End(xlUp).Row
Set R = Range("R3", "R" & 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 i


End Sub
2
  • change R.Rows.Count to R.Rows.Count -1 or 1 to 2 in your For i loop depending on whether you want to skip the bottom or top row. Commented Dec 21, 2018 at 19:20
  • This is not working as I had use [For i = R.Rows.Count - 1 To 1 Step -1] still adding same no of rows as the count in a cell. Commented Dec 21, 2018 at 19:39

2 Answers 2

1

I think by trying to insert the use of R as a Range is causing issues. It is not needed.

Sub InsertRowsIf()

Dim ws As Worksheet
Set ws = Worksheets("Sheet1") ' Change to your sheet

Dim lr As Long
lr = ws.Range("R" & Rows.Count).End(xlUp).Row

Application.ScreenUpdating = False

Dim i As Long
For i = lr To 3 Step -1
    If IsNumeric(ws.Cells(i, 18).Value) And ws.Cells(i, 18) <> "" Then
        ws.Cells(i, 1).Offset(1,0).Resize(ws.Cells(i, 18).Value - 1).EntireRow.Insert
    End If
Next i

Application.ScreenUpdating = True

End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

My file name is "new insert rows" andsheet name is Sheet1 by using this showing out of range Set ws = Worksheets("new insert rows")
1. Make sure you are using the latest version of my code above, I have made a few changes since first posting. 2. Make sure there are no spaces or other non printable characters that you did not include in the name in the vba. It would help if you copied the name from the sheet directly, or better yet use the code name.
Yes I am using the latest version of code, no error comes up but still adding same number of rows as the count
@Abhishek AH, now I get what you are asking. See Edit.
Yes this is working,also my part of code also working the only thing I have to do is change value to value -1 in last line to make it happen.Thanks a lot @Scott.
0

You forgot to turn ScreenUpdating back on. Updated code to skip last row and applied standard indentation.

Option Explicit

Sub InsertRowsIf()

Dim lr As Long, R As Range, i As Long

lr = Range("R" & Rows.Count).End(xlUp).Row
Set R = Range("R3:R" & lr - 1)

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 i
Application.ScreenUpdating = True

End Sub

1 Comment

Is it enough to add one less no of rows based on the count i have in a cell?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.