Insert Row if the Entire Row Cell has Zero

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

How can I insert a blank row if the entire row has zero ?

Before:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Track1[/TD]
[TD]Track2[/TD]
[TD]Track3[/TD]
[TD]Track4[/TD]
[/TR]
[TR]
[TD]Ben[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Sam[/TD]
[TD]3[/TD]
[TD]4[/TD]
[TD]5[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]Anna[/TD]
[TD]1[/TD]
[TD]0[/TD]
[TD]5[/TD]
[TD]9[/TD]
[/TR]
[TR]
[TD]Ted[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[/TR]
</tbody>[/TABLE]


After:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Track1[/TD]
[TD]Track2[/TD]
[TD]Track3[/TD]
[TD]Track4[/TD]
[/TR]
[TR]
[TD]Sam[/TD]
[TD]3[/TD]
[TD]4[/TD]
[TD]5[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]Ben[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Anna[/TD]
[TD]1[/TD]
[TD]0[/TD]
[TD]5[/TD]
[TD]9[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]ZEROES[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Ted[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[/TR]
</tbody>[/TABLE]


Thanks in advance for the help.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try this:

Code:
Option Explicit
Sub Macro2()
    
    Dim lngLastCol As Long
    Dim strLastCol As String
    Dim lngLastRow As Long
    Dim lngMyRow   As Long
    
    Application.ScreenUpdating = False
    
    lngLastCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    strLastCol = Left(Cells(1, lngLastCol).Address(True, False), Application.WorksheetFunction.Search("$", Cells(1, lngLastCol).Address(True, False)) - 1)
    lngLastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For lngMyRow = lngLastRow To 2 Step -1
        If Evaluate("SUM(B" & lngMyRow & ":" & strLastCol & lngMyRow & ")") = 0 Then
            Rows(lngMyRow).EntireRow.Insert
        End If
    Next lngMyRow
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
.. or possibly this
Code:
Sub insertRows()
  Dim a As Variant
  Dim i As Long
  Dim sTest As String
  
  Application.ScreenUpdating = False
  With Range("A1").CurrentRegion
    a = .Offset(, 1).Resize(, .Columns.Count - 1).Value
  End With
  sTest = Mid(Replace(String(UBound(a, 2), "0"), 0, "|0"), 2)
  For i = UBound(a) To 2 Step -1
    If Join(Application.Index(a, i, 0), "|") = sTest Then Rows(i).Insert
  Next i
  Application.ScreenUpdating = True
End Sub

I haven't addressed anything about the changed order of the rows shown as that wasn't asked about and we have no details about.
 
Last edited:
Upvote 0
Try this:
Robert, a comment on your code. It may not be possible with the OP's data but it would insert a blank row above a row that contained
3, 4, -1, -6

It would also insert a blank row above a row that was already blank in those 4 columns.

In both examples above the rows don't meet the OP's stated requirement.
 
Upvote 0
Good points. Thanks Peter :)
No problem. :)

One further tip. The red part is not required here (though it won't hurt) as Rows(lngMyRow) already is an entire row.
Rich (BB code):
Rows(lngMyRow).EntireRow.Insert
 
Upvote 0
Hi Peter,

Based on your kind advice, here's my revised code:

Code:
Option Explicit
Sub Macro2()
    
    Dim lngLastCol As Long
    Dim strLastCol As String
    Dim lngLastRow As Long
    Dim lngMyRow   As Long
    
    Application.ScreenUpdating = False
    
    lngLastCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    strLastCol = Left(Cells(1, lngLastCol).Address(True, False), Application.WorksheetFunction.Search("$", Cells(1, lngLastCol).Address(True, False)) - 1)
    lngLastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For lngMyRow = lngLastRow To 2 Step -1
        'If every column from column B to the last column are all zero, then...
        If Evaluate("COUNTIF(B" & lngMyRow & ":" & strLastCol & lngMyRow & ",0)") = lngLastCol - 1 Then
            '...insert a blank row
            Rows(lngMyRow).Insert
        End If
    Next lngMyRow
    
    Application.ScreenUpdating = True

End Sub

What do you think?

Thanks,

Robert
 
Upvote 0
I forgot to mention that each Track column should be sorted in descending order. Thanks Peter for the help :)
 
Upvote 0
Here is another macro that should also work...
Code:
[table="width: 500"]
[tr]
	[td]Sub InsertRowsAboveAllZeroes()
  Dim R As Long, Arr As Variant
  Arr = Split(Application.Trim(Join(Application.Transpose(Evaluate("IF(COUNTIF(OFFSET(B1,ROW(A1:A26)-1,0,1," & Cells(1, Columns.Count).End(xlToLeft).Column - 1 & "),""<>0""),"""",Row(A1:A26))")))))
  Application.ScreenUpdating = False
  For R = UBound(Arr) To 0 Step -1
    If Len(Arr(R)) Then Rows(Arr(R)).Insert
  Next
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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