delete old files from, folder and list, shift cells up under conditions.

BRB1983

Board Regular
Joined
Aug 29, 2019
Messages
61
the issue is that the code is is deleting more than i ask for. it also deletes a cell that is not colored.

Code:
Sub delete_test_2()
Dim MyFolder As String
Dim cell As Range
Application.ScreenUpdating = False
    With Sheets("Delete Revs")
    MyFolder = .Range("K1").Value & "\"
        For Each cell In .Range("C3:C17")
            If cell.Interior.Color = RGB(255, 255, 255) And cell.Offset(0, 1) = "A" Then
                Kill MyFolder & cell.Value & ".pdf"
                cell.Offset(0, 1).Delete xlShiftUp
                cell.Offset(0, 2).Delete xlShiftUp
                cell.Delete xlShiftUp
            End If
        Next cell
            For Each cell In .Range("C3:C17")
                If cell.Interior.Color = RGB(255, 255, 255) And cell.Offset(0, 1) = "0" Then
                    Kill MyFolder & cell.Value & ".pdf"
                    cell.Offset(0, 1).Delete xlShiftUp
                    cell.Offset(0, 2).Delete xlShiftUp
                    cell.Delete xlShiftUp
                End If
            Next cell
 
Last edited by a moderator:

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)
You are checking one cell, then telling to delete three cells:
Code:
                cell.Offset(0, 1).Delete xlShiftUp
                cell.Offset(0, 2).Delete xlShiftUp
                cell.Delete xlShiftUp
So it is deleting the cell that meets the conditions, and the two cells to the right of it.
 
Upvote 0
How about
Code:
Sub BRB1983()
Dim MyFolder As String
Dim cell As Range, Rng As Range
Application.ScreenUpdating = False
    With Sheets("Delete Revs")
    MyFolder = .Range("K1").Value & "\"
        For Each cell In .Range("C3:C17")
            If cell.Interior.Color = RGB(255, 255, 255) And cell.Offset(0, 1) = "A" Then
                Kill MyFolder & cell.Value & ".pdf"
                If Rng Is Nothing Then Set Rng = cell.Resize(, 3) Else Set Rng = Union(Rng, cell.Resize(, 3))
             ElseIf cell.Interior.Color = RGB(255, 255, 255) And cell.Offset(0, 1) = "0" Then
                 Kill MyFolder & cell.Value & ".pdf"
                If Rng Is Nothing Then Set Rng = cell.Resize(, 3) Else Set Rng = Union(Rng, cell.Resize(, 3))
             End If
         Next cell
         If Not Rng Is Nothing Then Rng.Delete xlUp
End Sub
 
Last edited:
Upvote 0
here is code from start to finish with the issues. also conditional formatting is involved.
Code:
Option Explicit

Sub LOAD_FILES_TO_DELETE_OLD_REVs()
Dim MyFolder As String
Dim MyFile As String
Dim a As Integer
Dim fldr As String
Dim LAStrow As Long
Dim strtcell As Range
Range("D2") = "REV"
Application.StatusBar = True
    Application.ScreenUpdating = False
fldr = Sheets("sheet1").Range("K1").Value
MyFolder = fldr
MyFile = Dir(MyFolder & "\" & "*.*")
a = 2
Do While MyFile <> ""
    a = a + 1
    Cells(a, 3).Value = Left(MyFile, Len(MyFile) - 4)
    MyFile = Dir
Loop
LAStrow = Range("C" & Rows.Count).End(xlUp).Row
Range("D3:D" & LAStrow).Formula = "=right(C3,1)"
Range("E3:E" & LAStrow).Formula = "=left(C3,len(C3)-6)"
'MsgBox "FILE LOADING COMPLETE."
Worksheets("sheet1").Columns("A:f").AutoFit
Application.StatusBar = False
    Application.ScreenUpdating = True
End Sub




Sub delete_test_2()
Dim MyFolder As String
Dim cell As Range
Application.ScreenUpdating = False
    With Sheets("sheet1")
    MyFolder = .Range("K1").Value & "\"
        For Each cell In .Range("C3:C17")
            If cell.Interior.Color = RGB(255, 255, 255) And cell.Offset(0, 1) = "A" Then
                Kill MyFolder & cell.Value & ".pdf"
                cell.Offset(0, 1).Delete xlShiftUp
                cell.Offset(0, 2).Delete xlShiftUp
                cell.Delete xlShiftUp
            End If
        Next cell
            For Each cell In .Range("C3:C17")
                If cell.Interior.Color = RGB(255, 255, 255) And cell.Offset(0, 1) = "0" Then
                    Kill MyFolder & cell.Value & ".pdf"
                    cell.Offset(0, 1).Delete xlShiftUp
                    cell.Offset(0, 2).Delete xlShiftUp
                    cell.Delete xlShiftUp
                End If
            Next cell
'                For Each cell In .Range("C3:C17")
'                    If cell.Interior.Color = RGB(255, 255, 255) And cell.Offset(0, 1) = "1" Then
'                        Kill MyFolder & cell.Value & ".pdf"
'                        cell.Offset(0, 1).Delete xlShiftUp
'                        cell.Offset(0, 2).Delete xlShiftUp
'                        cell.Delete xlShiftUp
'                    End If
'                Next cell
'                    For Each cell In .Range("C3:C17")
'                        If cell.Interior.Color = RGB(255, 255, 255) And cell.Offset(0, 1) = "2" Then
'                            Kill MyFolder & cell.Value & ".pdf"
'                            cell.Offset(0, 1).Delete xlShiftUp
'                            cell.Offset(0, 2).Delete xlShiftUp
'                            cell.Delete xlShiftUp
'                        End If
'                    Next cell
'                        For Each cell In .Range("C3:C17")
'                            If cell.Interior.Color = RGB(255, 255, 255) And cell.Offset(0, 1) = "3" Then
'                                Kill MyFolder & cell.Value & ".pdf"
'                                cell.Offset(0, 1).Delete xlShiftUp
'                                cell.Offset(0, 2).Delete xlShiftUp
'                                cell.Delete xlShiftUp
'                            End If
'                        Next cell
'                            For Each cell In .Range("C3:C17")
'                                If cell.Interior.Color = RGB(255, 255, 255) And cell.Offset(0, 1) = "4" Then
'                                    Kill MyFolder & cell.Value & ".pdf"
'                                    cell.Offset(0, 1).Delete xlShiftUp
'                                    cell.Offset(0, 2).Delete xlShiftUp
'                                    cell.Delete xlShiftUp
'                                End If
'                            Next cell
'                                For Each cell In .Range("C3:C17")
'                                    If cell.Interior.Color = RGB(255, 255, 255) And cell.Offset(0, 1) = "5" Then
'                                        Kill MyFolder & cell.Value & ".pdf"
'                                        cell.Offset(0, 1).Delete xlShiftUp
'                                        cell.Offset(0, 2).Delete xlShiftUp
'                                        cell.Delete xlShiftUp
'                                    End If
'                                Next cell
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Did you see my suggestion in post#4?
 
Upvote 0
yes just tried it said file not found so i blocked the kill file and it delete a file name from the list on the sheet that does not met the conditions.
each file has a REV. REV 0, REV 1, REV 2. Rev 2 being the newest one. so with that file name i need REV 0 & 1 to be deleted. there is one file, the first file name that only has one REV. so this one is not colored and it still deletes it.
 
Upvote 0
Do you have any merged cells?
 
Upvote 0
None merged.
I'm thinking my safest bet would try to first copy the file names that need to be deleted to a different column and then delete those files.
 
Upvote 0
In your previous thread you were looking for cells that were red, now you are looking for cells that do not have a fill colour, is that what you wanted?
 
Upvote 0

Forum statistics

Threads
1,223,445
Messages
6,172,177
Members
452,446
Latest member
walkman99

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