Solved Inserting row using condition.

October 30, 2014 at 23:16:08
Specs: Windows 7
I have a column data in which there are integer no. starting from 1. I want to insert rows conditionally when the number is greater than 1. The number of row which need to be inserted is equal to the number in column data.

See More: Inserting row using condition.

Report •

October 31, 2014 at 07:51:47
✔ Best Answer
There are a couple of pieces of information that are missing from your question.

1 - What Column are these integer values in?
2 - What Row do these integer values start in?

The following code makes a few assumptions:

1 - Your integer values are in Column A
2 - Your integer values start in Row 2, allowing for the use of Column headers
3 - There are no blank cells in the Column A, other than at the bottom of your list of integers
4 - There is nothing but integers in Column A

If these assumptions are not correct, the code will not work properly. Please clarify these assumptions if they are wrong.

Sub InsertRows()
    Dim n As Integer, m As Long, currentCell As Range
'Integer list begins in A2
    Set currentCell = Cells(2, "A")
'If Column A cell is not empty, determine number of rows to Insert
    Do While Not IsEmpty(currentCell)
        n = currentCell.Value
        m = currentCell.Row
'If n is > 1, insert rows and determine new currentCell
          If n > 1 Then
            Rows(m + 1 & ":" & m + n).Insert
            Set currentCell = currentCell.Offset(n + 1, 0)
'If n <= 1, increment currentCell and Loop
            Set currentCell = currentCell.Offset(1, 0)
          End If
End Sub

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.

Report •

November 7, 2014 at 04:04:02
Dear derby,

Thanks for your reply. it works.

Report •

Related Solutions

Ask Question