Insert new row, help

Babi_mn

New Member
Joined
Oct 4, 2023
Messages
12
Office Version
  1. 365
Platform
  1. Windows
I have 23019 row in my B column, and some of them are blank.
I want to add new blank row below each and every row except blank cells. Please help
PS i attached screenshot below (Column A is just numeric orders, ignore it please)
 

Attachments

  • Blank row.png
    Blank row.png
    79.4 KB · Views: 9

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Please try the following on a copy of your workbook.
VBA Code:
Option Explicit
Sub Insert_Rows()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<-- *** Change to actual sheet name ***
    Dim LRow As Long, LCol As Long, i As Long, j As Long
    LRow = ws.Cells(Rows.Count, "B").End(xlUp).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
    
    Dim a, b, c
    a = ws.Range(ws.Cells(2, 2), ws.Cells(LRow, 2))
    ReDim b(1 To UBound(a, 1), 1 To 1)
    ReDim c(1 To UBound(a, 1), 1 To 1)
    For i = 1 To UBound(a, 1) - 1
        If a(i, 1) <> "" Then
            b(i, 1) = j + 1
            j = j + 1
        Else
            b(i, 1) = j
        End If
    Next i
    ws.Cells(2, LCol).Resize(UBound(b, 1), 1).Value = b
    
    For i = 1 To UBound(b, 1) - 1
        If b(i, 1) <> b(i + 1, 1) And a(i, 1) <> "" Then
            c(i, 1) = b(i, 1)
        End If
    Next i
    ws.Cells(LRow + 1, LCol).Resize(UBound(c, 1), 1).Value = c
    
    LRow = ws.Cells(Rows.Count, LCol).End(xlUp).Row
    ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol)).Sort Key1:=ws.Cells(2, LCol), _
    order1:=xlAscending, Header:=xlNo
    ws.Columns(LCol).EntireColumn.ClearContents
    Application.ScreenUpdating = False
End Sub
 
Upvote 0
Please try the following on a copy of your workbook.
VBA Code:
Option Explicit
Sub Insert_Rows()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<-- *** Change to actual sheet name ***
    Dim LRow As Long, LCol As Long, i As Long, j As Long
    LRow = ws.Cells(Rows.Count, "B").End(xlUp).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
   
    Dim a, b, c
    a = ws.Range(ws.Cells(2, 2), ws.Cells(LRow, 2))
    ReDim b(1 To UBound(a, 1), 1 To 1)
    ReDim c(1 To UBound(a, 1), 1 To 1)
    For i = 1 To UBound(a, 1) - 1
        If a(i, 1) <> "" Then
            b(i, 1) = j + 1
            j = j + 1
        Else
            b(i, 1) = j
        End If
    Next i
    ws.Cells(2, LCol).Resize(UBound(b, 1), 1).Value = b
   
    For i = 1 To UBound(b, 1) - 1
        If b(i, 1) <> b(i + 1, 1) And a(i, 1) <> "" Then
            c(i, 1) = b(i, 1)
        End If
    Next i
    ws.Cells(LRow + 1, LCol).Resize(UBound(c, 1), 1).Value = c
   
    LRow = ws.Cells(Rows.Count, LCol).End(xlUp).Row
    ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol)).Sort Key1:=ws.Cells(2, LCol), _
    order1:=xlAscending, Header:=xlNo
    ws.Columns(LCol).EntireColumn.ClearContents
    Application.ScreenUpdating = False
End Sub
it is working , but it is not adding new row between number containing cell and blank cell, instead it just skipping it, could you please watch this?
 
Upvote 0
i mean, i need extra blank cell with old ones. If there was 1 blank then 2, was 2 then 3 etc...
 
Upvote 0
it is not adding new row between number containing cell and blank cell
No, because you said:
add new blank row below each and every row except blank cells
which is somewhat ambiguous. So is it the case that you want a new blank row except where the entire row is already blank? Or is it the case that you want a new blank row added even if A & B are blank - but only one more blank row if that is the case?
 
Upvote 0
No, because you said:

which is somewhat ambiguous. So is it the case that you want a new blank row except where the entire row is already blank? Or is it the case that you want a new blank row added even if A & B are blank - but only one more blank row if that is the case?
my bad, I need only one more blank row even there is blank already.
 
Upvote 0
So in your sample image - would you want a blank row below the "11" in column A - or not?
 
Upvote 0
OK, see if this does what you want. If it doesn't, then I'll need to see a before-and-after sample before I can progress any further.
VBA Code:
Option Explicit
Sub Insert_Rows_V2()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<-- *** Change to actual sheet name ***
    Dim LRow As Long, LCol As Long, i As Long, j As Long
    LRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
    
    Dim a, b
    a = ws.Range(ws.Cells(2, 1), ws.Cells(LRow, 1))
    ReDim b(1 To UBound(a, 1), 1 To 1)
    For i = 1 To UBound(a, 1) ' - 1
            b(i, 1) = j + 1
            j = j + 1
    Next i
    ws.Cells(2, LCol).Resize(UBound(b, 1), 1).Value = b
    LRow = ws.Cells(Rows.Count, LCol).End(xlUp).Row + 1
    ws.Cells(LRow, LCol).Resize(UBound(b, 1), 1).Value = b
    
    LRow = ws.Cells(Rows.Count, LCol).End(xlUp).Row
    ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol)).Sort Key1:=ws.Cells(2, LCol), _
    order1:=xlAscending, Header:=xlNo
    ws.Columns(LCol).EntireColumn.ClearContents
    Application.ScreenUpdating = False
End Sub
 
Upvote 0
I have 23019 row in my B column, and some of them are blank.
I want to add new blank row below each and every row except blank cells. Please help
PS i attached screenshot below (Column A is just numeric orders, ignore it please)

Try this on a copy of your data.

Make sure that the sheet is active.


VBA Code:
Private Sub subInsertBlankRow()
Dim i As Integer
Dim lngLastRow  As Long
    
    With ActiveSheet
        lngLastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
    End With
        
    For i = lngLastRow To 2 Step -1
        If Len(Cells(i, 2).Value) > 0 Then
            Cells(i, 2).Offset(1, 0).EntireRow.Insert
        End If
    Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,652
Messages
6,126,041
Members
449,281
Latest member
redwine77

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