+ Reply to Thread
Results 1 to 11 of 11

Delete rows if value in Column "X" is out of specific range.

  1. #1
    Registered User
    Join Date
    09-05-2017
    Location
    United Kingdom
    MS-Off Ver
    2016
    Posts
    22

    Delete rows if value in Column "X" is out of specific range.

    Hello everybody.

    I am trying to do a macro which deletes all rows in specific TAB in Excel Spreadsheet if value in Column A is smaller than 13420000000 and bigger than 16000000000. First row have to stay.

    I was trying to to use advance filtering and Kutools but with Kutools which kind of works but I can't record macro so the pure VBA probably will be my best bet.

    I was also digging in google good while but I can't find anything specific which would work.

    I am very new on macros so please be forgiving.

    Please help.

  2. #2
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: Delete rows if value in Column "X" is out of specific range.

    can you upload a small sample workbook so that i can see the data?

  3. #3
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: Delete rows if value in Column "X" is out of specific range.

    try something like this:

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    09-05-2017
    Location
    United Kingdom
    MS-Off Ver
    2016
    Posts
    22

    Re: Delete rows if value in Column "X" is out of specific range.

    It doesn't work I'm afraid Or I have edited macro incorrectly. Please see what I did:

    Sub DeleteRowsBasedOnCellValue()
    Dim wss As Worksheet, wst As Worksheet
    Dim lRow As Long
    Dim iCntr As Long
    Set wss = Sheets("DP")
    Set wst = Sheets("DP")

    With wst
    lRow = wst.Cells(wst.Rows.Count, "A").End(xlUp).Row
    For iCntr = lRow To 2 Step -1
    If wss.Range("A2") < 13420000000# Or wss.Range("A10000") > 16000000000# Then
    .Rows(iCntr).Delete
    End If
    Next
    End With
    End Sub


    This macro delete all rows. Probably minor adjustment is needed.

    Please find attached Delete PN - Before.xlsx showing spreadsheet before filtration and Delete PN - After.xlsx. "After" is the spreadsheet how I would like to see after applying macro.

    Please help.
    Attached Files Attached Files

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Delete rows if value in Column "X" is out of specific range.

    How about this.

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  6. #6
    Registered User
    Join Date
    09-05-2017
    Location
    United Kingdom
    MS-Off Ver
    2016
    Posts
    22

    Re: Delete rows if value in Column "X" is out of specific range.

    That works!

    Unfortunately is very slow to filter (takes approximately 5 minutes) but this is probably the nature of the task given to Excel. Is there any tricky way to speed it up?

    If not, I am glad having this code anyway! Thank you for that.

    Please tell me if possible to make it faster, if not I will mark post as solved anyway and I leave appropriate reputation comment,

    Cheers,

    Powerzasty.

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Delete rows if value in Column "X" is out of specific range.

    Maybe using Autofilter and then deleting visible rows might speed things up, but don't have time right now. Will try later today.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Delete rows if value in Column "X" is out of specific range.

    Try this. Had to put code in the file due to website firewall.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    09-05-2017
    Location
    United Kingdom
    MS-Off Ver
    2016
    Posts
    22

    Re: Delete rows if value in Column "X" is out of specific range.

    OMG! It looks like that simplest solutions are always the best solutions!

    That one works absolutely great!

    Thank you for help!

    Best regards,

    Powerzasty.

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Delete rows if value in Column "X" is out of specific range.

    You're welcome and thanks for rep+.

  11. #11
    Registered User
    Join Date
    09-05-2017
    Location
    United Kingdom
    MS-Off Ver
    2016
    Posts
    22

    Re: Delete rows if value in Column "X" is out of specific range.

    Hello again.

    Would be possible to use autoflter to select 5 different ranges?

    For example to keep numbers:

    12000000000 - 13000000000
    13420000000 - 13460000000
    14460000000 - 16000000000
    23450000000 - 24460000000
    34450000000 - 34460000000

    and then deletet rows which are out of these ranges ?

    Please advise.

    Best regards,

    Powerzasty.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 03-12-2017, 06:23 PM
  2. [SOLVED] Macro to Delete Rows in Columns Based on Length of Column "A"
    By rmmohan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-17-2013, 03:22 PM
  3. [SOLVED] Delete Rows if a cell does not equal "Finished" or "Complete"
    By Justair07 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 09-27-2013, 12:40 PM
  4. [SOLVED] Is it possible to a macro go row by row and hide (or delete) and rows where column 1 is ""
    By CDNcameron in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-20-2013, 02:14 PM
  5. Delete rows in the result of formulas "OK" n column "J"
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-29-2012, 10:08 AM
  6. Excel 2007 : Delete rows containing "NA" in column "A"
    By grsnipe in forum Excel General
    Replies: 4
    Last Post: 08-16-2011, 06:57 AM
  7. VBA Code for deleting rows where cell in column a states "delete row"
    By tnfire in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-20-2009, 06:17 PM

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