If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Deleting Rows using VB

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-05-07, 13:23
MillB MillB is offline
Registered User
 
Join Date: Dec 2006
Posts: 39
Deleting Rows using VB

Hello!

Can anyone give me VB code to delete a row if the cell value in a column = "0"? I'm new to the whole VB thing.
Based on the attached screenshot, I'm trying to remove the rows entirely if they have a zero in column E.

Much Thanks!

B
Attached Thumbnails
Deleting Rows using VB-aaa.png  
Reply With Quote
  #2 (permalink)  
Old 11-05-07, 14:58
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
Howdy. This should meet your needs:

Code:
Sub DeleteCriteriaTest()
    Dim n As Long, LastRow As Long
    Dim myCrit As Range
    LastRow = Range("E" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
    For n = LastRow To 2 Step -1
        Set myCrit = Cells(n, 5)
        If myCrit.Value = 0 Then Rows(n).EntireRow.Delete
    Next n
Application.ScreenUpdating = True
End Sub
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums

Last edited by shades; 11-05-07 at 15:02.
Reply With Quote
  #3 (permalink)  
Old 11-05-07, 16:07
MillB MillB is offline
Registered User
 
Join Date: Dec 2006
Posts: 39
Thanks!

Shades - I appreciate the help.
Thank you.
Reply With Quote
  #4 (permalink)  
Old 11-29-07, 10:57
pierrevbaexcel pierrevbaexcel is offline
Registered User
 
Join Date: Dec 2003
Location: Ottawa, Canada
Posts: 72
The solution could also be:

Sub proDelete()
Do Until Selection.Value = ""
If Selection.Value = 0 Then
Selection.EntireRow.Delete
Else
Selection.Offset(1, 0).Select
End If
Loop
End Sub
__________________
A piece of data is like a brick
If you don't build anything with it
It is just a brick
www.excel-examples.com
Reply With Quote
  #5 (permalink)  
Old 11-30-07, 11:49
DavidCoutts DavidCoutts is offline
Registered User
 
Join Date: Jan 2004
Location: Aberdeen, Scotland
Posts: 1,067
or yet even another way

Code:
Option Explicit

Sub runMacro()
    Call DeleteRowswithValue(Cells(1, 1), 0)
End Sub

Sub DeleteRowswithValue(rngColumn As Range, iValue As Integer)
    Dim rngActive As Range
    Dim bFoundCell As Boolean
    
    'initalise bfoundcell
    bFoundCell = True
    
    Do Until Not bFoundCell
        'attempt to find the value
        Set rngActive = rngColumn.EntireColumn.Find(what:=iValue, lookat:=xlWhole)
        If Not rngActive Is Nothing Then
            rngActive.EntireRow.Delete
        Else
            bFoundCell = False
        End If
    Loop
    
    Set rngfoundcell = Nothing
End Sub
btw piere your method will fail if there is a blank value in the cell.
Also i would advice strongly against using select or selections if at all possible.

There are loads of ways to do this but shades has given a good answer if there is a limit on the amount of data

Thanks
David
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On