Code to delete entire rows if zero in a particular column (after pasting data in a new tab)

AidanDanby

Board Regular
Joined
Jan 24, 2013
Messages
176
Office Version
  1. 365
Hi All,
Please can someone give me some code that will do the following:

1) Paste all data from Sheet1 into a new sheet.
2) Delete entire rows if there is a zero in column A

Cheers,

Aidan
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hello Aidan,

I dare say that we are going to need more information, however try the following as a starting point:-

VBA Code:
Sub Test()

    Application.ScreenUpdating = False
    
    Sheet1.UsedRange.Copy Sheet2.[A1]
    
    With Sheet2.[A1].CurrentRegion
            .AutoFilter 1, 0
            .Offset(1).EntireRow.Delete
            .AutoFilter
    End With
    
    Application.ScreenUpdating = True

End Sub

I'm assuming that your data starts in Sheet1 Row 2 with headings in Row 1.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
If your data is very large with lots of disjoint rows to delete, then this will be considerably faster. If the data is not very large then no particular benefit with this code.
I am also assuming a header row.

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

VBA Code:
Sub Test1()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  Sheets("Sheet1").Copy After:=Sheets("Sheet1")
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If a(i, 1) = 0 Then
      b(i, 1) = 1
      k = k + 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A2").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
Hi Vcoolio - thank you for your reply.
When I try and run the code it states 'Object Required' ?
 
Upvote 0
When I try and run the code it states 'Object Required' ?
When you report a vba error, please also report the line of code that produced the error.

@vcoolio's code worked for me.

I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
If your data is very large with lots of disjoint rows to delete, then this will be considerably faster. If the data is not very large then no particular benefit with this code.
I am also assuming a header row.

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

VBA Code:
Sub Test1()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  Sheets("Sheet1").Copy After:=Sheets("Sheet1")
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If a(i, 1) = 0 Then
      b(i, 1) = 1
      k = k + 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A2").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub
Hi Peter - I am getting a Run-time error '1004': Application-defined or object-defined error when I try and run the code?
 
Upvote 0
Hello Aidan,

In this case I can't say what would cause the error without seeing a sample of your worksheet(s). I'm thinking that your data starts further down the sheet rather than at Row 1.

I'd suggest trying Peter's code first just to see if the same error comes up. I used Peter's code on a dummy test sheet and it worked flawlessly.

Cheerio.
vcoolio.
 
Upvote 0
When you report a vba error, please also report the line of code that produced the error.

@vcoolio's code worked for me.

I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
How do I dee the line of code that producers the error?
 
Upvote 0
How do I dee the line of code that producers the error?
Do you get the option of a 'Debug' button? If so, click that and the offending line should be highlighted yellow. Please report the full error message and identify the code line.
Also make sure it is clear whose code you are referring to.
 
Upvote 0
Do you get the option of a 'Debug' button? If so, click that and the offending line should be highlighted yellow. Please report the full error message and identify the code line.
Also make sure it is clear whose code you are referring to.
1692016191678.png
 
Upvote 0

Forum statistics

Threads
1,215,242
Messages
6,123,830
Members
449,127
Latest member
Cyko

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