+ Reply to Thread
Results 1 to 23 of 23

Select Case and Delete rows

  1. #1
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Select Case and Delete rows

    I need some help fixing my errors. I am trying to automate a repetitive daily task.

    At present, a vlookup is used in column J to return values from another sheet, then column J is used as the basis for further actions to be done.
    For example, some rows are deleted based on the text shown in column J, and some cells in column I have the corresponding value in Column J pasted into them.

    I want to perform both these tasks using a macro.

    If it makes a difference - the values in column J are always vlookup formula results, rather than the text values which are shown in the sample file.

    Please Login or Register  to view this content.
    Thanks for your help
    Attached Files Attached Files
    Last edited by labrooy; 12-21-2011 at 05:30 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,964

    Re: Select Case and Delete rows

    Please Login or Register  to view this content.
    Ben Van Johnson

  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Select Case and Delete rows

    or with your code

    Please Login or Register  to view this content.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  4. #4
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Select Case and Delete rows

    Thanks for the quick reply. I added your code and tested it. It works but not quite how I want it. It does not delete the final row where column J has "Delete row".

    Also, I only want it to delete rows where the value starts with "Delete row -" and leave rows starting with "Delete row w". I tried to code this using what I thought was the wildcard * but it is not working.


    Please Login or Register  to view this content.

  5. #5
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010 & 2013
    Posts
    308

    Re: Select Case and Delete rows

    Hi

    Here's my version:
    Please Login or Register  to view this content.
    Best regards, Rob.

  6. #6
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Select Case and Delete rows

    Thanks pike, that did exactly what I needed, just had to add a hyphen in the appropriate place.

    Thank you both for the quick response and efforts.

  7. #7
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010 & 2013
    Posts
    308

    Re: Select Case and Delete rows

    Like I was way tooooo slloooowww.

  8. #8
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Select Case and Delete rows

    Thank you Rob for the alternative and all the useful explanations !

  9. #9
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Select Case and Delete rows

    never to slow you probably dont have broad band in sydney

  10. #10
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Select Case and Delete rows

    I am running the macro and getting a Runtime Error 13 Type mismatch

    In pike's code it is at line: If InStr(1, Range("J" & xrow).Value, "Delete row") > 0 Then

    In Rob's code it is at line: Case "CWA000000" To "CWA999999"

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Select Case and Delete rows

    Please Login or Register  to view this content.



  12. #12
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Select Case and Delete rows

    When you click a macro or you click a function on a menu that was created by another macro, you may receive the following error message:
    Runtime Error 13 Type mismatch
    have you changed the data from the example workbook?

  13. #13
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Select Case and Delete rows

    The data from the example workbook is different to the data in my real workbook -the real workbook has vlookup formula in the relevant cells.



    snb - thanks I will take a look at that alternative too.

  14. #14
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Select Case and Delete rows

    snb - your code does not work on the vlookup cells. I could change the vlookup values to text, but I would need to amend another macro to then add the vlookup again. I'm not sure if it is easier to amend one of the macro's everyone has provided, or copy/paste the vlookup results.

    It returns an error on line Columns(10).SpecialCells(4).EntireRow.Delete if there are no rows to delete. This should not be a problem as theoretically there will always be rows to delete.

    It also returns a run-time error 1004 Application/object defined error on line sn = Columns(10).SpecialCells(2).Resize(, 2)

  15. #15
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Select Case and Delete rows

    Hi labrooy,
    I cant replicate the errors

  16. #16
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Select Case and Delete rows

    I have managed to successfully run your code and Rob's code on a new sample file with vlookups included.

    On the real file, your code still gives me a Type 13 error, after successfully deleting all the "Delete row" values it gives the error on the same InStr line.

    Please Login or Register  to view this content.
    If it helps, the Yellow Debug highlighting stops at the end of Then

  17. #17
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Select Case and Delete rows

    can you zip and attach the problem workbook?

  18. #18
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Select Case and Delete rows

    just tried the code with 1000 lines an cant reproduce the error??

  19. #19
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Select Case and Delete rows

    Workbook attached. I have tested the 3 macro solutions in this workbook and receive errors for 2 of them and the other one does not seem to work as intended.
    Attached Files Attached Files

  20. #20
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Select Case and Delete rows

    The error in the cells is causing the problem

    use
    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    10-24-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Select Case and Delete rows

    Thanks pike.

    The new version deletes all the rows where column J = #N/A
    Last edited by labrooy; 12-22-2011 at 10:02 PM.

  22. #22
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Select Case and Delete rows

    cant remember if 2003 has iferror
    but if you change the formula to display blank in stead of #N/A
    Please Login or Register  to view this content.

  23. #23
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Select Case and Delete rows

    Please Login or Register  to view this content.

+ 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