Edit my code to delete sheets

levanoj

Active Member
Joined
Oct 25, 2007
Messages
311
Hello All,

The code below basically allows me to select a Directory, and then deletes all sheets of each file within that Directory that has ROWS 11, 13, 23 & 25 completely blank with no data in any of those cells.

What I'm looking to do is basically have this code do the same but instead delete all sheets with COLUMNS C, D, I & J that are completely blank. Can anyone help?

Code:
Sub TemplateTabRem3()
Dim i#, rng As Range, myFolder As String, fn As String, wf As WorksheetFunction
Set wf = Application.WorksheetFunction
With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show = True Then
        myFolder = .SelectedItems(1)
    Else
        Exit Sub
    End If
End With
fn = Dir(myFolder & "\*.xls")
'On Error Resume Next
Application.DisplayAlerts = False
Do While fn <> ""
    With Workbooks.Open(myFolder & "\" & fn)
        For i = .Worksheets.Count To 1 Step -1
            Set rng = Intersect(.Worksheets(i).[11:11,13:13,23:23,25:25], _
            .Worksheets(i).UsedRange)
                With rng
                   .Replace What:=Chr(32), replacement:="", LookAt:=xlPart
                   .Replace What:=Chr(160), replacement:="", LookAt:=xlPart
                End With
                If wf.CountBlank(rng.Areas(1)) + wf.CountBlank(rng.Areas(2)) + _
                       wf.CountBlank(rng.Areas(3)) + wf.CountBlank(rng.Areas(4)) _
                       = rng.Cells.Count Then .Worksheets(i).Delete
        Next
        .Close True
    End With
    fn = Dir
Loop
Application.DisplayAlerts = True
Set wf = Nothing
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
If it's of any help, I changed the code slightly as shown below:
Code:
Sub TemplateTabRem3()
Dim i#, rng As Range, myFolder As String, fn As String, wf As WorksheetFunction
Set wf = Application.WorksheetFunction
With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show = True Then
        myFolder = .SelectedItems(1)
    Else
        Exit Sub
    End If
End With
fn = Dir(myFolder & "\*.xls")
'On Error Resume Next
Application.DisplayAlerts = False
Do While fn <> ""
    With Workbooks.Open(myFolder & "\" & fn)
        For i = .Worksheets.Count To 1 Step -1
            Set rng = Intersect(.Worksheets(i).[C:C,D:D,I:I,J:J], _
            .Worksheets(i).UsedRange)
                With rng
                   .Replace What:=Chr(32), replacement:="", LookAt:=xlPart
                   .Replace What:=Chr(160), replacement:="", LookAt:=xlPart
                End With
                If wf.CountBlank(rng.Areas(1)) + wf.CountBlank(rng.Areas(2)) + _
                       wf.CountBlank(rng.Areas(3)) + wf.CountBlank(rng.Areas(4)) _
                       = rng.Cells.Count Then .Worksheets(i).Delete
        Next
        .Close True
    End With
    fn = Dir
Loop
Application.DisplayAlerts = True
Set wf = Nothing
End Sub

but got an error of "Run-time error '1004': Application-defined or object-defined error". When I clicked on Debug, I had the following highlighted:
Code:
If wf.CountBlank(rng.Areas(1)) + wf.CountBlank(rng.Areas(2)) + _
                       wf.CountBlank(rng.Areas(3)) + wf.CountBlank(rng.Areas(4)) _
                       = rng.Cells.Count Then

Any thoughts?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,971
Members
449,059
Latest member
oculus

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