Can anyone help with a script?

Excellis

New Member
Joined
Oct 30, 2012
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
Hi All
I'm just wondering if anyone could write a script for me to carry out a mundane task that tbh contains too many records to handle manually.
I am able to record macros and make simple edits but this is beyond my ken.
Ideally what I would like is to completely delete lines that fit certain criteria, or, if that can't be done, delete the contents so I can sort the rows with the blank ones at the end.
Attached is a very small sample which I believe contains all the scenarios that can and do occur.
The data is anonymised but in essence what I have is:
An extract from a LMS
2 types of vehicle a front loader & a side-saddle
2 types of training, novice & refresher, operators do a novice once followed after 3 years by a refresher and further refreshers thereafter
2 levels of operators, managers & colleagues
In the mini sheet I've indicated what action, in the sample, I need to happen.
The rules are:
Anyone with a Manager Status - delete the line
Any colleague that has completed a novice and a refresher, keep the refresher and delete the novice line
Any colleague that has completed a novice but hasn't yet completed a refresher (there will be no record for the refresher) keep the novice line
Any colleague that was supposed to complete a novice but didn't, delete the novice line
Regarding the coloured cells in pink, this is not the same colleague, it's a common name and there are 2 occurrences. They're distinguished by their clock number
In yellow, this IS the same colleague but he is proficient in both vehicles.
I'm unable to influence any of the data in the LMS so I have to work with this information.
I would also like the script to be portable across iterations of the report so if it could be run in the 'local window' (can't remember what it's officially called) that would be great.

I'm sure there will be further questions which I'm happy to answer.
Thank you in anticipation
Tom


Extract_File.xlsx
ABCDEFGHIJKL
1First NameLast NameClock NumberLevel/GradeCourse CodeLesson CodeLesson TitleExpiry dateLesson Completed DateDays out of dateAction required from the script
2JohnBates5500212ManagerB7-Front Loader54321B7-Front Loader Novice10/10/2019824DELETE - Due to Manager Status
3JohnBates5500212ManagerB7-Front LoaderB7FLRefB7-Front Loader Refresher9/10/202210/10/2019-271DELETE - Due to Manager Status
4MichaelBerrisford5500230Customer AssistantB7-Front Loader54321B7-Front Loader Novice16/11/20181152DELETE - Due to colleague has a refresher
5MichaelBerrisford5500230Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher15/11/202116/11/201857KEEP
6PeterClark5500248ManagerB7-Front LoaderB7FLRefB7-Front Loader Refresher8/04/20229/04/2019-87DELETE - Due to Manager Status
7SallyFluen5500257ManagerB7-Front Loader54321B7-Front Loader Novice18/06/2020572DELETE - Due to Manager Status
8SallyFluen5500257ManagerB7-Front LoaderB7FLRefB7-Front Loader Refresher18/06/202318/06/2020-523DELETE - Due to Manager Status
9JennySendi5500311Customer AssistantC17-Side Saddle678910C17-Side Saddle Novice9/07/2021186DELETE - Due to colleague has a refresher
10JennySendi5500311Customer AssistantC17-Side SaddleC17SSRefC17-Side Saddle Refresher8/07/20249/07/2021-909KEEP
11GeorgeFitzpatrick5500365Customer AssistantB7-Front Loader54321B7-Front Loader NoviceDELETE - Colleague did not complete his Novice (Col I is blank)
12MichaelEdwards5500392Customer AssistantC17-Side Saddle678910C17-Side Saddle Novice17/06/202418/06/2021207KEEP - Colleague has not needed to do a refresher
13WendyCarruthers5500401Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher10/11/202211/11/2019-303KEEP
14JohnBrown5500410Customer AssistantB7-Front Loader54321B7-Front Loader Novice22/10/202181DELETE - Due to colleague has a refresher
15JohnBrown5500410Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher21/10/202422/10/2021-1014KEEP
16BrianReilly5500428ManagerC17-Side SaddleC17SSRefC17-Side Saddle Refresher30/09/20241/10/2021-993DELETE - Due to Manager Status
17AdamFothergill5500437Customer AssistantB7-Front Loader54321B7-Front Loader Novice23/04/2020628DELETE - Due to colleague has a refresher
18AdamFothergill5500437Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher23/04/202323/04/2020-467KEEP
19JohnBrown5500455Customer AssistantB7-Front Loader54321B7-Front Loader Novice23/09/2020475DELETE - Due to colleague has a refresher
20JohnBrown5500455Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher23/09/202323/09/2020-620KEEP
21PaulClements5500473Customer AssistantB7-Front Loader54321B7-Front Loader Novice18/02/2021327DELETE - Due to colleague has a refresher
22PaulClements5500473Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher18/02/202418/02/2021-768KEEP
23MaryEvans5500491Customer AssistantB7-Front Loader54321B7-Front Loader Novice30/10/2020438DELETE - Due to colleague has a refresher
24MaryEvans5500491Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher30/10/202330/10/2020-657KEEP
25SamEwing5500509Customer AssistantB7-Front Loader54321B7-Front Loader NoviceDELETE - Colleague did not complete his Novice (Col I is blank)
26JohnJefferies5510201Customer AssistantB7-Front Loader54321B7-Front Loader Novice18/06/2020572DELETE - Due to colleague has a refresher
27JohnJefferies5510201Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher18/06/202318/06/2020-523KEEP
28JohnJefferies5510201Customer AssistantC17-Side Saddle678910C17-Side Saddle Novice9/07/2021186DELETE - Due to colleague has a refresher
29JohnJefferies5510201Customer AssistantC17-Side SaddleC17SSRefC17-Side Saddle Refresher8/07/20249/07/2021-909KEEP
30
31I WANT TO BE LEFT WITH:
32MichaelBerrisford5500230Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher15/11/202116/11/201857
33JennySendi5500311Customer AssistantC17-Side SaddleC17SSRefC17-Side Saddle Refresher8/07/20249/07/2021-909
34MichaelEdwards5500392Customer AssistantC17-Side Saddle678910C17-Side Saddle Novice17/06/202418/06/2021207
35WendyCarruthers5500401Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher10/11/202211/11/2019-303
36JohnBrown5500410Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher21/10/202422/10/2021-1014
37AdamFothergill5500437Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher23/04/202323/04/2020-467
38JohnBrown5500455Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher23/09/202323/09/2020-620
39PaulClements5500473Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher18/02/202418/02/2021-768
40MaryEvans5500491Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher30/10/202330/10/2020-657
41JohnJefferies5510201Customer AssistantB7-Front LoaderB7FLRefB7-Front Loader Refresher18/06/202318/06/2020-523
42JohnJefferies5510201Customer AssistantC17-Side SaddleC17SSRefC17-Side Saddle Refresher8/07/20249/07/2021-909
Extract
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
It looks to me that:
If column D is Manager, delete row
If column H is blank, delete row

If that is true then try this
VBA Code:
Sub Test()

Dim cell As Range, rngData As Range, rngDelete As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")   ' Change sheet name here if required
Set rngData = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    If ws.Range("D" & cell.Row) = "Manager" Then Set rngDelete = MarkDelete(cell, rngDelete)
    If ws.Range("H" & cell.Row) = "" Then Set rngDelete = MarkDelete(cell, rngDelete)
Next
rngDelete.EntireRow.Delete

End Sub

Function MarkDelete(c As Range, rng As Range) As Range
If rng Is Nothing Then Set MarkDelete = c Else Set MarkDelete = Union(rng, c)
End Function
 
Upvote 0
Solution
It looks to me that:
If column D is Manager, delete row
If column H is blank, delete row

If that is true then try this
VBA Code:
Sub Test()

Dim cell As Range, rngData As Range, rngDelete As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")   ' Change sheet name here if required
Set rngData = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    If ws.Range("D" & cell.Row) = "Manager" Then Set rngDelete = MarkDelete(cell, rngDelete)
    If ws.Range("H" & cell.Row) = "" Then Set rngDelete = MarkDelete(cell, rngDelete)
Next
rngDelete.EntireRow.Delete

End Sub

Function MarkDelete(c As Range, rng As Range) As Range
If rng Is Nothing Then Set MarkDelete = c Else Set MarkDelete = Union(rng, c)
End Function
Thank you, Zot. A classic example of not seeing the wood for the trees. With my limited knowledge and reading through your script I think you're right, but when I run the script I get an error:
Run-time '91': Object variable or With block variable not set, and when I click on debug this is highlighted: rngDelete.EntireRow.Delete
Not sure what that means.
 
Upvote 0
Thank you, Zot. A classic example of not seeing the wood for the trees. With my limited knowledge and reading through your script I think you're right, but when I run the script I get an error:
Run-time '91': Object variable or With block variable not set, and when I click on debug this is highlighted: rngDelete.EntireRow.Delete
Not sure what that means.
Realised my mistake, I forgot to change the column letters from my example to the actual sheet, I'd deleted some columns as they weren't pertinent to my request.
Instead of looking for cells that "=Manager" what do I need to do change it to cells that contain the word manager e.g. so it finds "Day Manager" or "Night Manager" and any other variation.
 
Upvote 0
You meant you want to delete if there is word Manager there?

If that is so, then change the line

VBA Code:
If ws.Range("D" & cell.Row) = "Manager" Then Set rngDelete = MarkDelete(cell, rngDelete)
to
VBA Code:
If ws.Range("D" & cell.Row) Like "*Manager" Then Set rngDelete = MarkDelete(cell, rngDelete)
 
Upvote 0
You meant you want to delete if there is word Manager there?
Yes, basically. In trying to simplify the example I think I over-simplified it. There are about 25 different job titles that contain the word Manager and I need to lose them all.
I ran your script in the example and that worked perfectly, thank you, I'm just trying to modify it to fit the real-world version I use.
 
Upvote 0
Let me modify to make it more customized
 
Upvote 0
Maybe can be more simplified but this is clear and easy to add any column and words.
VBA Code:
Sub Test()

Dim cell As Range, rngData As Range, rngDelete As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")   ' Change sheet name here if required
Set rngData = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    Select Case ws.Range("D" & cell.Row)
        Case "Manager", "Day Manager", "Night Manager"
            Set rngDelete = MarkDelete(cell, rngDelete)
    End Select
    Select Case ws.Range("H" & cell.Row)
        Case ""
            Set rngDelete = MarkDelete(cell, rngDelete)
    End Select
Next
rngDelete.EntireRow.Delete

End Sub

Function MarkDelete(c As Range, rng As Range) As Range
If rng Is Nothing Then Set MarkDelete = c Else Set MarkDelete = Union(rng, c)
End Function
 
Upvote 0
Another variation
VBA Code:
Sub Test()

Dim cell As Range, rngData As Range, rngDelete As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")   ' Change sheet name here if required
Set rngData = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    Select Case True
        Case ws.Range("D" & cell.Row) Like "*Manager"
            Set rngDelete = MarkDelete(cell, rngDelete)
        Case ws.Range("H" & cell.Row) = ""
            Set rngDelete = MarkDelete(cell, rngDelete)
    End Select
Next
rngDelete.EntireRow.Delete

End Sub
 
Upvote 0
Another variation
VBA Code:
Sub Test()

Dim cell As Range, rngData As Range, rngDelete As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")   ' Change sheet name here if required
Set rngData = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngData
    Select Case True
        Case ws.Range("D" & cell.Row) Like "*Manager"
            Set rngDelete = MarkDelete(cell, rngDelete)
        Case ws.Range("H" & cell.Row) = ""
            Set rngDelete = MarkDelete(cell, rngDelete)
    End Select
Next
rngDelete.EntireRow.Delete

End Sub
Apologies for being a pest. I now get a Compile error: Sub or function not defined, which refers to Set rngDelete = MarkDelete(cell, rngDelete). Is it something I'm doing wrong?
Macro error.JPG
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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