Solved

Simple Excel VBA

Posted on 2011-03-13
6
285 Views
Last Modified: 2012-06-21
I have a worksheet with a number of rows that will vary. I need a routine to delete all rows that do not have either an 11 or a 45 in the first cell of the row. The routine needs to stop when it gets to a blank row.
0
Comment
Question by:carlosab
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 35124778
In Column A?

A sample would help clarify yur questio

Cheers

Dave
0
 

Author Comment

by:carlosab
ID: 35124793
Yes, Column A. Here is a sample. Also, I need the script to delete any row where Column C does not start with CV. Thanks!
3      03-1-04      CV-0000059-11
3      03-1-04      CV-0000059-11
3      03-1-04      CV-0000060-11
11      11-1-01      CV-0000107-11
11      11-1-01      CV-0000108-11
11      11-1-01      CV-0000109-11
11      11-1-01      LT-0000098-11
43      43-3-02      LT-0000071-11
43      43-3-03      CV-0000043-11
43      43-3-03      CV-0000044-11
43      43-3-03      CV-0000045-11
43      43-3-03      CV-0000047-11
43      43-3-03      CV-0000047-11
45      45-1-05      LT-0000088-11
45      45-1-05      LT-0000089-11
45      45-1-05      LT-0000090-11
45      45-1-05      LT-0000090-11
0
 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
ID: 35124827
hth

sample file attached

Cheers

Dave
Sub CullRows()
    Dim rng1 As Range
    Dim lngRow As Long
    Application.ScreenUpdating = False
    If [a2] <> vbNullString Then
        Set rng1 = Range([a1], [a1].End(xlDown))
    Else
        Set rng1 = [a1]
    End If
    For lngRow = rng1.Rows.Count To 1 Step -1
        If Not (Cells(lngRow, "A") = "11" Or Cells(lngRow, "A") = "45") Then
            Rows(lngRow).EntireRow.Delete
        Else
            If Left$(Cells(lngRow, "c"), 2) <> "CV" Then Rows(lngRow).EntireRow.Delete
        End If
    Next
    Application.ScreenUpdating = True
End Sub

Open in new window

cull.xlsm
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:carlosab
ID: 35124929
This is my dataset before running your code:

3      3/1/2004      CV-0000059-11
3      3/1/2004      CV-0000059-11
3      3/1/2004      CV-0000060-11
11      11/1/2001      CV-0000107-11
11      11/1/2001      CV-0000108-11
11      11/1/2001      CV-0000109-11
11      11/1/2001      LT-0000098-11
43      43-3-02      LT-0000071-11
43      43-3-03      CV-0000043-11
43      43-3-03      CV-0000044-11
43      43-3-03      CV-0000045-11
43      43-3-03      CV-0000047-11
43      43-3-03      CV-0000047-11
45      45-1-05      LT-0000088-11
45      45-1-05      LT-0000089-11
45      45-1-05      LT-0000090-11
45      45-1-05      LT-0000090-11


This is the remaining data after I run it:

11      11/1/2001      CV-0000107-11
11      11/1/2001      CV-0000108-11
11      11/1/2001      CV-0000109-11


0
 

Author Comment

by:carlosab
ID: 35124937
Sorry, I didn't realize that 45 was all LT in the 3rd column. Code works perfect. Thanks!
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 35124942
thx for the grade :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question