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
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Something like this?
VBA Code:
Sub myFunction()
  Dim lRow As Integer
  lRow = Cells(Rows.Count, 1).End(xlUp).Row

  For i = lRow To 1 Step -1
    If Left(Cells(i, 1).Value, 5) <> "Stage" Then
      Cells(i, 1).EntireRow.Delete
    Else
      For j = 1 To 9
        Cells(i, 1).EntireRow.Insert
      Next
    End If
  Next
End Sub
 
Upvote 0
Hi Flashbond,
Thx for your input. In your code we delete also the 9 free lines and that may not happen because there is also soms conditional formatting in those lines..
(i will answer back tomorrow because i got to leave now)
 
Upvote 0
VBA Code:
Sub myFunction()
  Dim lRow As Integer
  lRow = Cells(Rows.Count, 1).End(xlUp).Row

  For i = 10 To lRow
    If Left(Cells(i, 1).Value, 5) = "Stage" Then
      Do While Left(Cells(i+10, 1).Value, 5) <> "Stage"
        Cells(i+10, 1).EntireRow.Delete
      Loop
    Else
      Range(Cells(i, 1), Cells(i, 3)).ClearContents 'Let say you have 3 columns of data
    End If
  Next
End Sub
Something similar to this must do the work. I can not say anything about the tables without seeing the layout.
 
Upvote 0
VBA Code:
Sub myFunction()
  Dim lRow As Integer
  lRow = Cells(Rows.Count, 1).End(xlUp).Row

  For i = 10 To lRow
    If Left(Cells(i, 1).Value, 5) = "Stage" Then
      Do While Left(Cells(i+10, 1).Value, 5) <> "Stage" Or Cells(i+10, 1).Value <> ""
        Cells(i+10, 1).EntireRow.Delete
      Loop
    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,

maybe

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

Const cstrSEARCH As String = "Stage"
Const clngDist As Long = 10

For lngRow = 10 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.Delete
  Set rngDel = Nothing
End If

For lngRow = 11 To Cells(Rows.Count, "A").End(xlUp).Row Step 10
  Range(Cells(lngRow, "A"), Cells(lngRow + 8, "A")).ClearContents
Next lngRow
End Sub

Ciao,
Holger
 
Upvote 0
Hi, sorry for late answer..

@Flashbond, the code (both) stays in the loop so i have to stop Excel and re-open..

@Holger, the code seems to work but it is only deleting the rows from columnA and not the whole row. Is it possible to delete the whole extra rows because there can also be data in col B,C,D,...

Thanks to re-evaluate
 
Upvote 0
Hi VeKa27,

change code at the end to read

Rich (BB code):
If Not rngDel Is Nothing Then
  rngDel.EntireRow.Delete
  Set rngDel = Nothing
End If

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

That seems to works fine! (happy)
Last request: is it possible to keep the data in the first line under each stage? I see now that it will be better for me to keep this data in those lines..
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,752
Members
448,989
Latest member
mariah3

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