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,

code worked in my sample workbook.

Original
MrE_1222736_1614512_vba search specific_221121.xlsm
ABCDEFGH
1
2
3
4
5Stage 1
6to keepto keepto keepto keepto keepto keepto keepto keep
7text
8text
9text
10text
11text
12text
13texttexttexttexttexttexttexttext
14text
15text
16text
17text
18text
19text
20Stage 2
21to keepto keepto keepto keepto keepto keepto keepto keep
22text
23texttexttexttexttexttexttexttext
24texttexttexttexttexttexttexttext
25texttexttexttexttexttexttexttext
26texttexttexttexttexttexttexttext
27texttexttexttexttexttexttexttext
28text
29text
30text
31text
32Stage 3
33to keepto keepto keepto keepto keepto keepto keepto keep
34texttexttexttexttexttexttexttext
35texttexttexttexttexttexttexttext
36Stage 4
37to keepto keepto keepto keepto keepto keepto keepto keep
38texttexttexttexttexttexttexttext
39texttexttexttexttexttexttexttext
40texttexttexttexttexttexttexttext
41texttexttexttexttexttexttexttext
42texttexttexttexttexttexttexttext
43texttexttexttexttexttexttexttext
44texttexttexttexttexttexttexttext
45texttexttexttexttexttexttexttext
46texttexttexttexttexttexttexttext
47texttexttexttexttexttexttexttext
48texttexttexttexttexttexttexttext
49Stage 5
50to keepto keepto keepto keepto keepto keepto keepto keep
51text
52text
53text
54text
55Stage 6
56to keepto keepto keepto keepto keepto keepto keepto keep
57texttexttexttexttexttexttexttext
58texttexttexttexttexttexttexttext
59texttexttexttexttexttexttexttext
60texttexttexttexttexttexttexttext
61texttexttexttexttexttexttexttext
62texttexttexttexttexttexttexttext
63texttexttexttexttexttexttexttext
64texttexttexttexttexttexttexttext
65texttexttexttexttexttexttexttext
66texttexttexttexttexttexttexttext
67texttexttexttexttexttexttexttext
Test


After running the code:
MrE_1222736_1614512_vba search specific_221121.xlsm
ABCDEFGH
1
2
3
4
5Stage 1
6to keepto keepto keepto keepto keepto keepto keepto keep
7
8
9
10
11Stage 2
12to keepto keepto keepto keepto keepto keepto keepto keep
13
14
15
16
17Stage 3
18to keepto keepto keepto keepto keepto keepto keepto keep
19
20
21
22
23Stage 4
24to keepto keepto keepto keepto keepto keepto keepto keep
25
26
27
28
29Stage 5
30to keepto keepto keepto keepto keepto keepto keepto keep
31
32
33
34
35Stage 6
36to keepto keepto keepto keepto keepto keepto keepto keep
37
38
39
40
41
42
43
44
Test_221124_124657


Slightly modified code used:
VBA Code:
Public Sub MrE_1222736_161480C_mod()
' 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
Const cblnHaHoBe    As Boolean = False

lngStart = clngStart + 1
var = lngStart

If cblnHaHoBe Then
  Forum1614516.Copy after:=Sheets(Sheets.Count)
  ActiveSheet.Name = "Test" & Format(Now, "_yymmdd_hhmmss")
End If

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 + 1, "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 + 1, "A"), Cells(lngStart + clngDist - 2, "A")).EntireRow.ClearContents
    Else
      Range(Cells(lngStart + 1, "A"), Cells(lngStart + clngDist - 2, "A")).EntireRow.ClearContents
    End If
    lngStart = lngStart + clngDist
  Else
    Range(Cells(lngStart + 1, "A"), Cells(Rows.Count, "A").End(xlUp)).EntireRow.ClearContents
    Exit Do
  End If
Loop
Application.ScreenUpdating = True
End Sub

Ciao,
Holger
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I have to see your sample data.
Before the code run:
1669290686720.png

After code runs once
1669290741145.png

B
 
Upvote 0
Oh, I see.. In real life is your data starting from Column A or B?
Try this. It will search for Stage1, Stage2, etc.. in column B:
VBA Code:
Sub myFunction()
  Dim lRow As Integer
  lRow = Cells(Rows.Count, 2).End(xlUp).Row

  For i = 5 To lRow
    If Left(Cells(i, 2).Value, 5) = "Stage" Then
      If Left(Cells(i + 6, 2).Value, 5) <> "Stage" Then
        Cells(i + 6, 2).EntireRow.Delete
        If Cells(i + 6, 2).Value <> "" Then
          i = i - 1
        End If
      End If
    Else
      If Left(Cells(i-1, 2).Value, 5) <> "Stage" Then
        Range(Cells(i, 2), Cells(i, 5)).ClearContents
      End If
    End If
  Next
End Sub
 
Upvote 0
It is my testing file... please keep searching in column A because in real life it is other data then "stage" in column B (sorry for misunderstanding)
 
Upvote 0
Ok, please try post #24 code in your test file. You'll see it is working.
Then you can use the code in post #17 in real file.
That's all I can do, sorry..
 
Upvote 0
Ok, no problem... like you say, in test file it works but in real life not..
I will figure it out somehow.. :)
Thanks a lot for your time..
 
Upvote 0
Hi VeKa27,

maybe you should not pass pictures but use XL2BB to display the contents of your real data as it seems that the information given to us is different to what you have in your file. Glass marble is broken, Mindreading not available at present - at least from my side .

Holger
 
Upvote 0
Try the below

VBA Code:
Sub deleterows()



' select the correct sheet
'Sheets(Sheets.Count).Activate


Range("A10").Select
If ActiveCell.Offset(10, 0).Value <> "Stage 2" Then
ActiveCell.Offset(10, 0).Select
Do
Rows(ActiveCell.Row).Delete
Loop While ActiveCell.Value <> "Stage 2"
End If

For x = 11 To 19
Rows(x).Clear
Next x
Range("A20").Select
If ActiveCell.Offset(10, 0).Value <> "Stage 3" Then
ActiveCell.Offset(10, 0).Select
Do
Rows(ActiveCell.Row).Delete
Loop While ActiveCell.Value <> "Stage 3"
End If

For x = 21 To 29
Rows(x).Clear
Next x

Range("A30").Select
If ActiveCell.Offset(11, 0).Value <> "" Then
ActiveCell.Offset(10, 0).Select
Do
Rows(ActiveCell.Row).Delete
Loop While ActiveCell.Value <> ""

End If
For x = 31 To 39
Rows(x).Clear
Next x
 
Upvote 0
Hi VeKa27,

a little bit of information about the loops and time for each:

Rich (BB code):
Flashbond
Looped for 339 times on 215 rows.
Elapsed time 0,31640625 sec

Code is using 2 loops:
Rich (BB code):
HaHoBe old
Looped for 225 times on 215 rows.
Elapsed time 0,03125 sec

Rich (BB code):
HaHoBe new
Looped for 15 times on 215 rows.
Elapsed time 0,0625 sec

No check for code from I know nuffin: restricted to only 3 stages

Another way would be to load data as array into memory, process and rewrite array to worksheet. But that would require precise information and not a model for test which diesn't suit live data.

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,525
Members
449,088
Latest member
RandomExceller01

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