VBA: Delete Row if Value is Zero & Copy Data Based on Values

unknownymous

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

Hope all is well.

I have a sheet with more than more or less 100k lines. I need to trim the data first by omitting rows with "0" or blank as Value in Row R (Sheet 1). I found below code but it's taking much time probably because the file is too large.

Sub DeleteRow() 'DeleteRow ot Value is Zero

Dim MyRange As Range
Set MyRange = Range("R1:R500000")

Do
Set myCell = MyRange.Find("0", LookIn:=xlValues)
If Not myCell Is Nothing Then
myCell.EntireRow.Delete
End If
Loop While Not myCell Is Nothing
End Sub


Once done, I need to create 2 tab sheets on the same file namely - "Team P" and "Team N". Data is given in is Sheet 1 column B

Team P sheet - Copy those positive change from Sheet 1 (column B)
Team N sheet - Copy those negative change from Sheet 1 (column B)

I found and tweak this code but its taking too much time.

Sub CopyPositive ()

Dim i As Range
For Each i In Range("B1:B250000")
If i.Value > 0 Then
i.Select
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
Sheets("Team P").Range("A250000").End(xlUp).Offset(1, 0).PasteSpecial
End If
Next i
End Sub

Source: Copy rows to other sheet if cell value is greater than zero - OzGrid Free Excel/VBA Help Forum

Any thoughts will be much appreciated. :)
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
What is the last used column & do you have a header row in row 1?
 
Upvote 0
Thanks for that, how about
VBA Code:
Sub unknownymous()
   Dim Ary As Variant, Nary As Variant
   Dim R As Long, i As Long, NxtCol As Long

   Ary = Range("R2:R" & Range("B" & Rows.Count).End(xlUp).Row).Value2
   ReDim Nary(1 To UBound(Ary), 1 To 1)
   For R = 1 To UBound(Ary)
      If Ary(R, 1) = "" Or Ary(R, 1) = 0 Then
         Nary(R, 1) = 1
         i = i + 1
      End If
   Next R
   If i > 0 Then
      Application.ScreenUpdating = False
      With Range("A2").Resize(UBound(Nary), 23)
         .Columns(23).Value = Nary
         .Sort .Columns(23), xlAscending, , , , , , xlNo
         .Resize(i).EntireRow.Delete
      End With
   End If
End Sub
 
Upvote 0
Thanks for that, how about
VBA Code:
Sub unknownymous()
   Dim Ary As Variant, Nary As Variant
   Dim R As Long, i As Long, NxtCol As Long

   Ary = Range("R2:R" & Range("B" & Rows.Count).End(xlUp).Row).Value2
   ReDim Nary(1 To UBound(Ary), 1 To 1)
   For R = 1 To UBound(Ary)
      If Ary(R, 1) = "" Or Ary(R, 1) = 0 Then
         Nary(R, 1) = 1
         i = i + 1
      End If
   Next R
   If i > 0 Then
      Application.ScreenUpdating = False
      With Range("A2").Resize(UBound(Nary), 23)
         .Columns(23).Value = Nary
         .Sort .Columns(23), xlAscending, , , , , , xlNo
         .Resize(i).EntireRow.Delete
      End With
   End If
End Sub
Thanks for this Fluff. I missed something in my scenario. I need to pivot first Sheet 1 since there is repetitive accounts (get sum change). Column to pivot is R (value) and column E as Account Name.

Once the data is ready, the macro will check account name based on values: Let's say we have Pivoted tab Sheet
Step 1: Macro will check names with positive change and lookup the names in Sheet 1 and copy the data in Sheet 3.
Step 2: Macro will check names with negative change and lookup the names in Sheet 1 and copy the data in Sheet 4.

It's kinda bizaare but appreciate the your insight. :)
 
Upvote 0
That has absolutely nothing to do with your original question, which was about speeding the deleting of rows which were 0 or blank.
 
Upvote 0
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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