Clear certain cells

JOEE1979

Active Member
Joined
Dec 18, 2022
Messages
264
Office Version
  1. 365
Platform
  1. Windows
Hello everyone
UNITMechanicW/O #ActivityClassificationStatus
1212Anthony Ayou4525bootBDNOn Hold
1236joan52455engine8646Completed
2122jim654541415151On Hold
4152Bratish Panjara48258556446Running RepairTSS
I'm stumped,
At the end of the day I would like to clear the contents of the line that has "completed" in the last column. and I would like for the table to auto sort.
The "status" column is a drop down if that makes a difference.
Keep in mind this part of my report is 74 lines long.
This spreadsheet has more columns and rows beside and under what you see, (so I do not want to delete rows for those reasons).
I need help please and thank you.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
A couple of questions
1. which columns are required to be cleared...as there are no visible headers ?
2. which column is the data to be sorted by ?
 
Upvote 0
Hi. Try the below in a normal module...

VBA Code:
Sub EndODay()

'declares active worksheet
Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet

'create loop through column F (assuming that's where the Status col. is)
Dim c As Range
Dim lrow As Long: lrow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row
For Each c In ws.Range("F2:F" & lrow)
    If c.Value = "Completed" Then
        ws.Rows(c.Row).EntireRow.ClearContents
    End If
Next c

'Sort by column F
With ws.Sort
    .SortFields.Clear
    .SortFields.Add2 Key:=ws.Range("F2:F" & lrow), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange ws.UsedRange
    .Header = xlYes
    .Orientation = xlTopToBottom
    .Apply
End With

End Sub
 
Upvote 0
Untitled.jpg

On the green section, I would like to clear the "completed" line, to be sorted by "status" column (without interrupting any of the other sections).
The section is goes to line 74. Thank you
 
Upvote 0
Hi. Try the below in a normal module...

VBA Code:
Sub EndODay()

'declares active worksheet
Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet

'create loop through column F (assuming that's where the Status col. is)
Dim c As Range
Dim lrow As Long: lrow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row
For Each c In ws.Range("F2:F" & lrow)
    If c.Value = "Completed" Then
        ws.Rows(c.Row).EntireRow.ClearContents
    End If
Next c

'Sort by column F
With ws.Sort
    .SortFields.Clear
    .SortFields.Add2 Key:=ws.Range("F2:F" & lrow), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange ws.UsedRange
    .Header = xlYes
    .Orientation = xlTopToBottom
    .Apply
End With

End Sub
 
Upvote 0
.SortFields.Add2 Key:=ws.Range("F2:F" & lrow), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal

It didnt work, the above was highlighted to be debuged
 
Upvote 0
A couple of questions
1. which columns are required to be cleared...as there are no visible headers ?
2. which column is the data to be sorted by ?
Untitled.jpg


On the green section, I would like to clear the "completed" line, to be sorted by "status" column (without interrupting any of the other sections).
The section is goes to line 74. Thank you
 
Upvote 0
.SortFields.Add2 Key:=ws.Range("F2:F" & lrow), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal

It didnt work, the above was highlighted to be debuged
Thanks for the image. The below has been updated so the first row starts on row 10 (row with headers). The clearcontents macro will now be limited to the active row and columns A through F.

VBA Code:
Sub EndODay()

'declares active worksheet
Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet

'create loop through column F (assuming that's where the Status col. is)
Dim c As Range
Dim lrow As Long: lrow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row
For Each c In ws.Range("F10:F" & lrow)
    If c.Value = "Completed" Then
        'will clear the active row's columns A through F
        ws.Range(ws.Cells(c.Row, 1), ws.Cells(c.Row, 6)).ClearContents
    End If
Next c

'Sort by column F
With ws.Sort
    .SortFields.Clear
    .SortFields.Add2 Key:=ws.Range("F10:F" & lrow), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange ws.UsedRange
    .Header = xlYes
    .Orientation = xlTopToBottom
    .Apply
End With

End Sub
 
Upvote 0
Thanks for the image. The below has been updated so the first row starts on row 10 (row with headers). The clearcontents macro will now be limited to the active row and columns A through F.

VBA Code:
Sub EndODay()

'declares active worksheet
Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet

'create loop through column F (assuming that's where the Status col. is)
Dim c As Range
Dim lrow As Long: lrow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row
For Each c In ws.Range("F10:F" & lrow)
    If c.Value = "Completed" Then
        'will clear the active row's columns A through F
        ws.Range(ws.Cells(c.Row, 1), ws.Cells(c.Row, 6)).ClearContents
    End If
Next c

'Sort by column F
With ws.Sort
    .SortFields.Clear
    .SortFields.Add2 Key:=ws.Range("F10:F" & lrow), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange ws.UsedRange
    .Header = xlYes
    .Orientation = xlTopToBottom
    .Apply
End With

End Sub
This is an improvement, 3 issues;
1. it don't stop at line 74, it keeps going
2. it deletes the "Completed" line (which is good) but it does not sort and move everything (so it leaves a blank line)
3. the debug still highlights as previous
 
Upvote 0
Maybe, but it would have been easier if you had posted the dat rather than an image.....using XL2BB...see my sig for downloading same.

VBA Code:
Sub EndODay()
'declares active worksheet
Dim ws As Worksheet
'create loop through column F (assuming that's where the Status col. is)
Dim c As Range
For Each c In ws.Range("F10:F74")
    If c.Value = "Completed" Then
        'will clear the active row's columns A through F
        Range(ws.Cells(c.Row, 1), ws.Cells(c.Row, 6)).Delete
    End If
Next c

'Sort by column F
With ws.Sort
    .SortFields.Clear
    .SortFields.Add2 Key:=ws.Range("F10:F74"), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange ws.UsedRange
    .Header = xlYes
    .Orientation = xlTopToBottom
    .Apply
End With

End Sub
 
Upvote 0

Similar threads

Forum statistics

Threads
1,216,095
Messages
6,128,795
Members
449,468
Latest member
AGreen17

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