+ Reply to Thread
Results 1 to 4 of 4

Deleting Rows using a range in Column A

  1. #1
    Registered User
    Join Date
    08-25-2005
    Posts
    7

    Deleting Rows using a range in Column A

    I regularly receive a file of financial data and it contains 4 digit cost center numbers in column A. There are cost center numbers in which I am not interested and would like to use a macro to delete the rows which contain the numbers in column A in the range 5600 to 6500.

    The code I'm using loops so I would appreciate any help.

    'Start Test Conditional Delete
    Range("A1:A1").Select

    Dim rng As Range
    Dim i As Integer, counter As Integer

    'Set the range to evaluate to rng.

    'initialize i to 1
    i = 1

    'Loop for a count of 1 to the number of rows in the range that you want to evaluate.
    For counter = 1 To rng.Rows.Count

    'If cell i in the range contains a cost center number between 5600 and 6500
    delete the row.

    If rng.Cells(i) < 6500 Or rng.Cells(i) > 5599 Then
    rng.Cells(i).EntireRow.Delete
    Else
    i = i + 1
    End If
    Next

  2. #2
    Registered User
    Join Date
    08-24-2005
    Location
    Philippines
    Posts
    75

    Smile

    your code works if you change

    If rng.Cells(i) < 6500 Or rng.Cells(i) > 5599

    to

    If rng.Cells(i) < 6500 And rng.Cells(i) > 5599


    Quote Originally Posted by leskoby
    I regularly receive a file of financial data and it contains 4 digit cost center numbers in column A. There are cost center numbers in which I am not interested and would like to use a macro to delete the rows which contain the numbers in column A in the range 5600 to 6500.

    The code I'm using loops so I would appreciate any help.

    'Start Test Conditional Delete
    Range("A1:A1").Select

    Dim rng As Range
    Dim i As Integer, counter As Integer

    'Set the range to evaluate to rng.

    'initialize i to 1
    i = 1

    'Loop for a count of 1 to the number of rows in the range that you want to evaluate.
    For counter = 1 To rng.Rows.Count

    'If cell i in the range contains a cost center number between 5600 and 6500
    delete the row.

    If rng.Cells(i) < 6500 Or rng.Cells(i) > 5599 Then
    rng.Cells(i).EntireRow.Delete
    Else
    i = i + 1
    End If
    Next

  3. #3
    Bob Phillips
    Guest

    Re: Deleting Rows using a range in Column A

    Dim i As Long
    Dim iLastRow As Long

    iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = iLastRow To 1 Step -1
    If Cells(i, "A").Value < 6500 And Cells(i, "A").Value > 5599 Then
    Rows(i).Delete
    End If
    Next i


    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "leskoby" <[email protected]> wrote in
    message news:[email protected]...
    >
    > I regularly receive a file of financial data and it contains 4 digit
    > cost center numbers in column A. There are cost center numbers in which
    > I am not interested and would like to use a macro to delete the rows
    > which contain the numbers in column A in the range 5600 to 6500.
    >
    > The code I'm using loops so I would appreciate any help.
    >
    > 'Start Test Conditional Delete
    > Range("A1:A1").Select
    >
    > Dim rng As Range
    > Dim i As Integer, counter As Integer
    >
    > 'Set the range to evaluate to rng.
    >
    > 'initialize i to 1
    > i = 1
    >
    > 'Loop for a count of 1 to the number of rows in the range that you
    > want to evaluate.
    > For counter = 1 To rng.Rows.Count
    >
    > 'If cell i in the range contains a cost center number between
    > 5600 and 6500
    > delete the row.
    >
    > If rng.Cells(i) < 6500 Or rng.Cells(i) > 5599 Then
    > rng.Cells(i).EntireRow.Delete
    > Else
    > i = i + 1
    > End If
    > Next
    >
    >
    > --
    > leskoby
    > ------------------------------------------------------------------------
    > leskoby's Profile:

    http://www.excelforum.com/member.php...o&userid=26616
    > View this thread: http://www.excelforum.com/showthread...hreadid=398939
    >




  4. #4
    Registered User
    Join Date
    08-25-2005
    Posts
    7

    Thanks, both sets of code worked.

    Thanks, both sets of code worked.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1