VBA search specific lines and delete them

VeKa27

Board Regular
Joined
Sep 11, 2015
Messages
56
Hi you all. I need some help in this problem i have..
Imagine a clean excel sheet. In cell A10 i have Text "Stage 1". In cell A20 i have Text "Stage 2". In cell A30 i have Text "Stage 3".
Under every Stage i put some data in the 9 free lines.
Each day i make a reset to delete all data in the 9 free lines to start the day clean.
But now comes the problem..
Sometimes i insert new lines because i had not enough lines. If i reset the next day, i delete the inserted lines again because i only want 9 free lines under every Stage.
Now i'm searching to insert a code to search the cells with the Stages and delete the lines under every stage exept the 9 first ones. I cannot delete the Stages lines themselves.
Below the Stages there is a Table that i also cannot delete or touch.
It is just a question how to delete the new inserted lines and the data in the 9 lines under every Stage.
Thanks a lot to help me with this one
 
Hi VeKa27,

change codelines at the end to

Rich (BB code):
For lngRow = 11 To Cells(Rows.Count, "A").End(xlUp).Row Step 10
  Range(Cells(lngRow + 1, "A"), Cells(lngRow + 8, "A")).EntireRow.ClearContents
Next lngRow
Ciao,
Holger
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi Holger,

Great! It works as well now. I'm gonna implement this code in my original document.
In this document i have also the same sheet but there are the stages on Line5, 11, 17 (instead of 10 lines it is 6 lines)
To make your code work for that sheet, do i only have to change the step from 10 to 6 ?
(i am very greatfull for your support)
 
Upvote 0
Here is a working version of my code starting from 5th row and leaving only 6 rows.
VBA Code:
Sub myFunction()
  Dim lRow As Integer
  lRow = Cells(Rows.Count, 1).End(xlUp).Row

  For i = 5 To lRow
    If Left(Cells(i, 1).Value, 5) = "Stage" Then
      If Left(Cells(i + 6, 1).Value, 5) <> "Stage" Then
        Cells(i + 6, 1).EntireRow.Delete
        If Cells(i + 6, 1).Value <> "" Then
          i = i - 1
        End If
      End If
    Else
      Range(Cells(i, 1), Cells(i, 3)).ClearContents 'Let say you have 3 columns of data
    End If
  Next
End Sub
 
Upvote 0
Hi VeKa27,

as there are some hard coded distances regarding the first posting maybe try this

VBA Code:
Public Sub MrE_1222736_1614512_Vers221124_01()
' https://www.mrexcel.com/board/threads/vba-search-specific-lines-and-delete-them.1222736/
Dim lngRow          As Long
Dim lngBetween      As Long
Dim rngDel          As Range

Const cstrSEARCH    As String = "Stage"
Const clngDist      As Long = 6
Const clngStart     As Long = 5

For lngRow = clngStart To Cells(Rows.Count, "A").End(xlUp).Row
  If Left(Cells(lngRow, "A").Value, Len(cstrSEARCH)) = cstrSEARCH Then
    lngBetween = 0
  End If
  lngBetween = lngBetween + 1
  If lngBetween > clngDist Then
    If rngDel Is Nothing Then
      Set rngDel = Cells(lngRow, "A")
    Else
      Set rngDel = Union(rngDel, Cells(lngRow, "A"))
    End If
  End If
Next lngRow

If Not rngDel Is Nothing Then
  rngDel.EntireRow.Delete
  Set rngDel = Nothing
End If

For lngRow = clngStart To Cells(Rows.Count, "A").End(xlUp).Row Step clngDist
  Range(Cells(lngRow + 1, "A"), Cells(lngRow + clngDist - 1, "A")).EntireRow.ClearContents
Next lngRow
End Sub

Please note that the code will not work correctly if there are less rows between the searched items.

Holger
 
Upvote 0
Hi VeKa27,

here is a different approach which would also insert any missing lines between the searched items (and of course delete the ones that had been added):

VBA Code:
Public Sub MrE_1222736_161480C()
' https://www.mrexcel.com/board/threads/vba-search-specific-lines-and-delete-them.1222736/
' Code works on ActiveSheet
Dim lngStart        As Long
Dim var             As Variant

Const cstrSEARCH    As String = "Stage"
Const clngDist      As Long = 6
Const clngStart     As Long = 5
  
lngStart = clngStart + 1
var = lngStart

Application.ScreenUpdating = False
Do Until Cells(lngStart, "A").Value = ""
  var = Application.Match(cstrSEARCH & "*", Range(Cells(lngStart, "A"), Cells(Rows.Count, "A").End(xlUp)), 0)
  If IsNumeric(var) Then
    If lngStart + var - 1 > lngStart + clngDist Then
      Range(Cells(lngStart + clngDist - 1, "A"), Cells(lngStart + var - 2, "A")).EntireRow.Delete xlShiftUp
      Range(Cells(lngStart, "A"), Cells(lngStart + clngDist - 2, "A")).EntireRow.ClearContents
    ElseIf lngStart + var < lngStart + clngDist Then
      Cells(lngStart + var - 1, "A").Resize(clngDist - var, 1).EntireRow.Insert xlShiftDown
      Range(Cells(lngStart, "A"), Cells(lngStart + clngDist - 2, "A")).EntireRow.ClearContents
    Else
      Range(Cells(lngStart, "A"), Cells(lngStart + clngDist - 2, "A")).EntireRow.ClearContents
    End If
    lngStart = lngStart + clngDist
  Else
    Range(Cells(lngStart, "A"), Cells(Rows.Count, "A").End(xlUp)).EntireRow.ClearContents
    Exit Do
  End If
Loop
Application.ScreenUpdating = True
End Sub

Ciao,
Holger
 
Upvote 0
It should keep the first line after "Stage "
VBA Code:
Sub myFunction()
  Dim lRow As Integer
  lRow = Cells(Rows.Count, 1).End(xlUp).Row

  For i = 5 To lRow
    If Left(Cells(i, 1).Value, 5) = "Stage" Then
      If Left(Cells(i + 6, 1).Value, 5) <> "Stage" Then
        Cells(i + 6, 1).EntireRow.Delete
        If Cells(i + 6, 1).Value <> "" Then
          i = i - 1
        End If
      End If
    Else
      If Left(Cells(i-1, 1).Value, 5) <> "Stage" Then
        Range(Cells(i, 1), Cells(i, 3)).ClearContents 'Let say you have 3 columns of data
      End If
    End If
  Next
End Sub
 
Upvote 0
@Holger: Your last code does not do anything anymore?

@Flashbond: Code works but if there are mor then 1 extra line, the code deletes the lines only one by one (i have to repeat the code untill all extra lines are gone)
 
Upvote 0
It deletes every extra line when I run it.
Let say you have Stage at line 5 and you have data upto row 12. It will delete row 12 and 11 until the next Stage reaches to row 11.
 
Upvote 0
@Flashbond: If i run the code, it only deletes row12. If i run it again, it deletes row11.
The same thing happens with the other stages, only one by one and have to repeat the code
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,830
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top