+ Reply to Thread
Results 1 to 16 of 16

loop to delete certain text and stop if cannot find

  1. #1
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    40

    Lightbulb loop to delete certain text and stop if cannot find

    Hello every one
    i am still new at Macro and i am trying to do a code to search for text "BOX" and delete the cells which is many at the columns, shifting up after deleting and when finish stop, but actually after deleting the cells I am facing error number '91'
    can any one help please.BOX.jpg

    my code is

    Sub Test3()

    For i = 3 To 10000
    If Cells.Find(What:="BOX").Select Then
    Selection.Delete Shift:=xlUp
    End If
    Next

    End Sub

  2. #2
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    917

    Re: loop to delete certain text and stop if cannot find

    When deleting it's easier if you start bottom up:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    40

    Re: loop to delete certain text and stop if cannot find

    thanks for your notice but still the same error.

  4. #4
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    917

    Re: loop to delete certain text and stop if cannot find

    I know, you need another approach, this will do what you are going for:
    Please Login or Register  to view this content.
    Last edited by rollis13; 06-28-2022 at 09:09 AM.

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: loop to delete certain text and stop if cannot find

    It will delete only those rows that contain "BOX" in column A.
    If "BOX" occurs in other columns, they will not be deleted.

    Please Login or Register  to view this content.
    The loop
    Please Login or Register  to view this content.
    will execute unnecessarily 9997 times even if all cells containing "BOX" have already been deleted.

    Artik

  6. #6
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    40

    Re: loop to delete certain text and stop if cannot find

    thanks very much its working - more over i want to delete the "BOX" row and (two rows above it) and (3 rows under it) thanks for your patient
    before iam using some think like the following code but now i cannot adjust with your formula



    Dim StartRange As String
    Dim EndRange As String
    For i = 3 To 10000
    If Cells.Find(What:="BOX").Select Then
    Selection.Offset(-2, 0).Select
    StartRange = ActiveCell.Address
    Selection.Offset(5, 0).Select
    EndRange = ActiveCell.Address
    ActiveSheet.Range(StartRange & ":" & EndRange).Select
    Selection.Delete Shift:=xlUp
    End If
    Next

    End Sub

  7. #7
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: loop to delete certain text and stop if cannot find

    If "BOX" occurred in row 4, what should be done? Still delete rows 2 and 3, or should they be protected?

    Artik

  8. #8
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    40

    Re: loop to delete certain text and stop if cannot find

    no need to be removed
    please see the attached picture you will get what i want
    All the highlighted gray rows need to be removed

    Attachment 785448

  9. #9
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: loop to delete certain text and stop if cannot find

    The image is not visible. Attach it according to the yellow banner at the top of this page.

    Artik

  10. #10
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    40

    Re: loop to delete certain text and stop if cannot find

    sorry i think this one will come box 2.jpg

  11. #11
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: loop to delete certain text and stop if cannot find

    Please Login or Register  to view this content.
    Artik

  12. #12
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    40

    Re: loop to delete certain text and stop if cannot find

    Sorry but nothing happened please find attached excel sample
    Attached Files Attached Files

  13. #13
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: loop to delete certain text and stop if cannot find

    Remove this code fragment:
    Please Login or Register  to view this content.
    Artik

  14. #14
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    40

    Re: loop to delete certain text and stop if cannot find

    thank a lot -- its working finally , i am really appreciating your efforts.

    thank you

  15. #15
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: loop to delete certain text and stop if cannot find

    The whole thread should have been started by posting a workbook, not pictures, which in addition did not reflect the actual layout of the data. We unnecessarily beat the foam for several posts
    If your problem has been solved, please mark the thread as SOLVED.

    Artik

  16. #16
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    40

    Re: loop to delete certain text and stop if cannot find

    thanks again for your help (Solved)

+ 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. [SOLVED] find a text in a range, delete it and delete the value in cell to its left
    By snkhan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-12-2020, 07:18 PM
  2. Loop through a find and delete
    By Dominic.Brice in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-02-2018, 11:25 AM
  3. [SOLVED] VB Loop different variables and stop variable to find a new variable..
    By feroguz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-14-2014, 04:54 PM
  4. [SOLVED] find the part of text and delete the row and loop continues
    By vimalanathk in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-03-2014, 11:23 AM
  5. Find data and delete remaining rows (For each loop)
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-24-2009, 11:45 AM
  6. Find and delete loop question
    By crowdx42 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-18-2006, 09:29 PM
  7. [SOLVED] [SOLVED] find, delete, repeat loop
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2006, 02:25 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