Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 25th, 2007, 08:10 AM
Registered User
 
Join Date: Mar 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Deleting rows not equal to specific values

Hi,

How can I create a macro that deletes rows when a cell is not equal to specific values e.g

1
2
3
4
5
6
7
8
9
10

If the cells above is not equal to 2,4,5, and 7, then delete rows.

Hope you can help

 
Old March 25th, 2007, 11:14 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Here is one primitve way of doing this

Sub DeleteRows()

lmax = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell) .Row
For i1 = 1 To lmax

 If (Cells(i1, 1).Value <> 2 And Cells(i1, 1).Value <> 4 And Cells(i1, 1).Value <> 5 And Cells(i1, 1).Value <> 7) Then

        ActiveSheet.Rows(i1).EntireRow.Delete
        i1 = i1 - 1
        lmax = lmax - 1
        If i1 >= lmax Then Exit Sub
 End If
Next i1
End Sub

I have assumed that ur values will be in first column

Cheers
Shasur

http://www.vbadud.blogspot.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Speed up macro - deleting rows icabo Excel VBA 1 October 8th, 2007 02:18 PM
Highlight specific rows in a gridview rao965 ASP.NET 2.0 Professional 6 June 14th, 2007 03:20 AM
Insert row between specific rows Paula222 Access VBA 2 February 10th, 2006 03:56 PM
equal spacing b/w rows in datagrid possible? Renu ASP.NET 1.0 and 1.1 Basics 14 September 20th, 2004 04:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.