|
本帖最后由 yygpdkkk 于 2013-6-16 00:39 编辑
---------------
- Sub 遍历文件夹删除空白工作表()
- Dim thePath$, theBook As Workbook, sht As Object
- Dim theVisibleShtCount&, theSht As Worksheet, theStr$
- Dim theBookCount&, theShtCount&
- With Application.FileDialog(msoFileDialogFolderPicker)
- .AllowMultiSelect = False
- If .Show = -1 Then
- thePath = .SelectedItems(1)
- If Right(thePath, 1) <> "" Then thePath = thePath & ""
- Else
- If MsgBox("以本工作簿所在路径为默认文件夹吗?" _
- & vbNewLine & vbNewLine & "单击“否”可退出程序", vbYesNo, "确认默认路径") = vbYes Then
- thePath = ThisWorkbook.Path
- If Right(thePath, 1) <> "" Then thePath = thePath & ""
- Else
- GoTo The_Exit
- End If
- End If
- End With
- theStr = Dir(thePath & "*.xls")
- If theStr <> "" Then
- Application.ShowWindowsInTaskbar = False
- Do
- Application.ScreenUpdating = False
- If theStr <> ThisWorkbook.Name Then
- On Error Resume Next
- Set theBook = Workbooks.Open(thePath & theStr)
- If Err.Number = 0 Then
- On Error GoTo 0
- theBookCount = theBookCount + 1
- For Each theSht In theBook.Worksheets
- If WorksheetFunction.CountA(theSht.Cells) = 0 Then
- theVisibleShtCount = 0
- If theBook.Sheets.Count > 1 Then
- For Each sht In theBook.Sheets
- If sht.Visible = xlSheetVisible Then theVisibleShtCount = theVisibleShtCount + 1
- Next sht
- If theVisibleShtCount > 1 Then
- Application.DisplayAlerts = False
- theSht.Delete
- theShtCount = theShtCount + 1
- Application.DisplayAlerts = True
- Else
- If theSht.Visible = xlSheetVisible Then
- Application.ScreenUpdating = True
- MsgBox "工作簿内须至少含有一张可视工作表" _
- & vbNewLine & vbNewLine & "当前待处理工作簿“" & theBook.Name & "”的最后一个可视工作表“" & theSht.Name & "”不能被删除!", vbExclamation, "警告"
- Application.ScreenUpdating = False
- End If
- End If
- Else
- Application.ScreenUpdating = True
- MsgBox "工作簿内须至少含有一张可视工作表" _
- & vbNewLine & vbNewLine & "当前待处理工作簿“" & theBook.Name & "”的最后一个可视工作表“" & theSht.Name & "”不能被删除!", vbExclamation, "警告"
- Application.ScreenUpdating = False
- End If
- End If
- Next theSht
- theBook.Close SaveChanges:=True
- Else
- On Error GoTo 0
- Application.ScreenUpdating = True
- MsgBox "打开工作簿" & theStr & "失败!", vbCritical, "错误"
- Application.ScreenUpdating = False
- End If
- End If
- theStr = Dir
- Loop Until theStr = ""
- Application.ScreenUpdating = True
- MsgBox "共计处理 " & theBookCount & " 个工作簿,删除 " & theShtCount & " 个工作表", vbInformation, "提示"
- Application.ShowWindowsInTaskbar = True
- Else
- Application.ScreenUpdating = True
- MsgBox "不存在目标工作簿!", vbInformation, "提示"
- End If
- The_Exit:
- Application.ScreenUpdating = True
- Application.ShowWindowsInTaskbar = True
- Set theBook = Nothing
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|