How to delete rows and tabs

zombiemaster

Board Regular
Joined
Oct 27, 2009
Messages
241
I used to know some of this stuff but have been out of the VBA game for a few years...sorry to have to ask such a simple question!

I have a workbook that gets created each day with varying numbers of rows each day. Column C will have a numeric value. I need to delete all the rows with a numeric value greater than zero.

I currently do this manually by sorting A>Z, find the first row in the list with a number greater than zero, then deleting all the rows below that, but want to include VBA to do this step for me.

ALSO, there are multiple tabs in the workbook, and I delete all of them except the first tab. Is there a way to delete all the tabs except the first one named "Original Data"?

Thanks for your help,
-=ZM=-
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Thanks. In the overall scale of Excel, that is a fairly small number, so give this a try with a copy of your data.
I have assumed a header row

VBA Code:
Sub DelPositiveRows()
  Application.ScreenUpdating = False
  With Range("C2", Range("C" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(#),if(#>0,True,#),if(#="""","""",#))", "#", .Address))
    On Error Resume Next
    .SpecialCells(xlConstants, xlLogical).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks. In the overall scale of Excel, that is a fairly small number, so give this a try with a copy of your data.
I have assumed a header row

VBA Code:
Sub DelPositiveRows()
  Application.ScreenUpdating = False
  With Range("C2", Range("C" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(#),if(#>0,True,#),if(#="""","""",#))", "#", .Address))
    On Error Resume Next
    .SpecialCells(xlConstants, xlLogical).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.ScreenUpdating = True
End Sub
Unfortunately, nothing happened...tried it a couple of times, but it didn't do anything at all. Thoughts?

-=ZM=-
 
Upvote 0
Unfortunately, nothing happened...tried it a couple of times, but it didn't do anything at all. Thoughts?
A likely possibility is that those numbers are not actual numbers but text. What happens if you put this formula in a vacant cell?
Where I have C5, make sure that it is point at one of the "numbers" greater than zero in column C
=ISNUMBER(C5)
 
Upvote 0
A likely possibility is that those numbers are not actual numbers but text. What happens if you put this formula in a vacant cell?
Where I have C5, make sure that it is point at one of the "numbers" greater than zero in column C
=ISNUMBER(C5)
Yes, you're right - it comes up as FALSE. I changed the format of the column to Number but it didn't help...until I double-clicked in C5 and pressed enter, then it updated to TRUE. I have code somewhere to do that automatically for a range, but that was years ago that I had it, and have changed jobs since then so probably don't have access to the code...
 
Upvote 0
Leave them as text and try this instead.

Rich (BB code):
Sub DelPositiveRows()
  Application.ScreenUpdating = False
  With Range("C2", Range("C" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(#+0),if(#+0>0,True,#),if(#="""","""",#))", "#", .Address))
    On Error Resume Next
    .SpecialCells(xlConstants, xlLogical).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Leave them as text and try this instead.

Rich (BB code):
Sub DelPositiveRows()
  Application.ScreenUpdating = False
  With Range("C2", Range("C" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(#+0),if(#+0>0,True,#),if(#="""","""",#))", "#", .Address))
    On Error Resume Next
    .SpecialCells(xlConstants, xlLogical).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.ScreenUpdating = True
End Sub
That did the trick, thank you!!

Also, if you have time to look at the 2nd part of my issue, I have multiple tabs and only need to keep the one called "Original Data" and delete any others.
Thanks!
-=ZM=-
 
Upvote 0
Two codes below. The first deletes all sheets except the first. The second code deletes all sheets except "Original Data". Second would be safest I would say in case the order of sheets happens to have been changed.

VBA Code:
Sub DeleteSheets_1()
  Dim i As Long
  
  Application.DisplayAlerts = False
    For i = Sheets.Count To 2 Step -1
      Sheets(i).Delete
    Next i
  Application.DisplayAlerts = True
End Sub

Sub DeleteSheets_2()
  Dim ws As Worksheet
  
  Application.DisplayAlerts = False
    For Each ws In Worksheets
      If ws.Name <> "Original Data" Then ws.Delete
    Next ws
  Application.DisplayAlerts = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,459
Messages
6,124,947
Members
449,198
Latest member
MhammadishaqKhan

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