VBA: Separate & Add Row if All Column Has Zero

unknownymous

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

I'm trying to create a macro where cell that has "0" value will be separated.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Code[/TD]
[TD]Name[/TD]
[TD]Score 1[/TD]
[TD]Score 2[/TD]
[TD]Score 3[/TD]
[TD]Score 4[/TD]
[TD]Score 5[/TD]
[TD]Score 6[/TD]
[/TR]
[TR]
[TD]01[/TD]
[TD]John[/TD]
[TD]98[/TD]
[TD]98[/TD]
[TD]95[/TD]
[TD]95[/TD]
[TD]93[/TD]
[TD]90[/TD]
[/TR]
[TR]
[TD]02[/TD]
[TD]Ben[/TD]
[TD]96[/TD]
[TD]97[/TD]
[TD]94[/TD]
[TD]93[/TD]
[TD]93[/TD]
[TD]90[/TD]
[/TR]
[TR]
[TD]03[/TD]
[TD]Sean[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]04[/TD]
[TD]Carl[/TD]
[TD]90[/TD]
[TD]0[/TD]
[TD]90[/TD]
[TD]0[/TD]
[TD]89[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]05[/TD]
[TD]Tim[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[/TR]
</tbody>[/TABLE]

End result:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Code[/TD]
[TD]Name[/TD]
[TD]Score 1[/TD]
[TD]Score 2[/TD]
[TD]Score 3[/TD]
[TD]Score 4[/TD]
[TD]Score 5[/TD]
[TD]Score 6[/TD]
[/TR]
[TR]
[TD]01[/TD]
[TD]John[/TD]
[TD]98[/TD]
[TD]98[/TD]
[TD]95[/TD]
[TD]95[/TD]
[TD]93[/TD]
[TD]90[/TD]
[/TR]
[TR]
[TD]02[/TD]
[TD]Ben[/TD]
[TD]96[/TD]
[TD]97[/TD]
[TD]94[/TD]
[TD]93[/TD]
[TD]93[/TD]
[TD]90[/TD]
[/TR]
[TR]
[TD]04[/TD]
[TD]Carl[/TD]
[TD]90[/TD]
[TD]0[/TD]
[TD]90[/TD]
[TD]0[/TD]
[TD]89[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]03[/TD]
[TD]Sean[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]05[/TD]
[TD]Tim[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[/TR]
</tbody>[/TABLE]



Any help will be much appreciated.

Thank you!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Give this macro a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub MoveZeroRows()
  Dim LastRow As Long, LastCol As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
  Application.ScreenUpdating = False
  With Range(Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1))
    .Formula = "=IF(SUM(C2:" & Cells(2, LastCol).Address(0, 0) & ")=0,99999,ROW(C2))"
    .Value = .Value
    .Offset(, -LastCol).Resize(, LastCol + 1).Sort Cells(1, LastCol + 1), xlAscending
  End With
  On Error Resume Next
  Columns(LastCol + 1).Find(99999, , xlValues, xlWhole, , xlNext, , , False).EntireRow.Insert
  On Error GoTo 0
  Columns(LastCol + 1).Delete
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Hi Rick,

I noticed that whenever I add 2 blank columns in between Score 1 & Score 2, column Score 1 column with data is separated together with the zeroes. How can we modify this? Thank you.
 
Upvote 0
I noticed that whenever I add 2 blank columns in between Score 1 & Score 2, column Score 1 column with data is separated together with the zeroes. How can we modify this? Thank you.

I am not sure what you are asking for here... when I do what you describe, the results look like I would expect them to. Can you describe the problem you are seeing in more detail (perhaps post tables like you did in Message #1 )?
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,152
Members
452,615
Latest member
bogeys2birdies

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