Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
12
返回列表 发新帖
楼主: 张雄友

[已解决]去除空白工作表

[复制链接]
 楼主| 发表于 2015-2-11 21:29 | 显示全部楼层
hwc2ycy 发表于 2015-2-11 21:14
剩下你于测试当工作簿是3个空白表的情况吧。

如果有很多(如100个)空白工作簿,会弹出很多错误对话框的。
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2015-2-11 21:33 | 显示全部楼层
张雄友 发表于 2015-2-11 21:29
如果有很多(如100个)空白工作簿,会弹出很多错误对话框的。

不报错不就行了,搞得这么纠结,或者在代码完行完成后再集中出一个错误的汇总。


回复

使用道具 举报

发表于 2015-2-11 21:37 | 显示全部楼层
既然是指量处理文件,当然不可能中间还来一个对话框需要交互的,这不是与自动化批量处理相背离嘛,难道代码在运行的时候还要人守着不停的点确定。要输出错误,可以用DUBUG或者错误写在代码所在的工作簿里。
回复

使用道具 举报

 楼主| 发表于 2015-2-11 21:39 | 显示全部楼层
hwc2ycy 发表于 2015-2-11 21:37
既然是指量处理文件,当然不可能中间还来一个对话框需要交互的,这不是与自动化批量处理相背离嘛,难道代码 ...

    Application.DisplayAlerts = False
    On Error Resume Next

这本来就有的,还是会显示错误对话框。
回复

使用道具 举报

 楼主| 发表于 2015-2-11 21:43 | 显示全部楼层
hwc2ycy 发表于 2015-2-11 21:37
既然是指量处理文件,当然不可能中间还来一个对话框需要交互的,这不是与自动化批量处理相背离嘛,难道代码 ...

MsgBox err.Description ' 是这句吗??????????????????????????????????????不行哦。还请明示。
  1. Sub 删除空白工作表()
  2. On Error GoTo 100
  3. Dim thePath$$$$, theBook As Workbook, sht As Object
  4. Dim theVisibleShtCount&, theSht As Worksheet, theStr
  5. Dim theBookCount&, theShtCount&
  6. With Application.FileDialog(msoFileDialogFolderPicker)
  7. .AllowMultiSelect = False
  8. If .Show = -1 Then
  9. thePath = .SelectedItems(1)
  10. If Right(thePath, 1) <> "" Then thePath = thePath & ""
  11. Else
  12. If MsgBox("以本工作簿所在路径为默认文件夹吗?" _
  13. & vbNewLine & vbNewLine & "单击“否”可退出程序", vbYesNo, "确认默认路径") = vbYes Then
  14. thePath = ThisWorkbook.Path
  15. If Right(thePath, 1) <> "" Then thePath = thePath & ""
  16. Else
  17. GoTo The_Exit
  18. End If
  19. End If
  20. End With

  21. ' If theStr <> "" Then
  22. Application.ShowWindowsInTaskbar = False
  23. Application.ScreenUpdating = False
  24. Application.DisplayAlerts = False

  25. If theStr <> ThisWorkbook.Name Then
  26. On Error Resume Next
  27. theStr = GetName(thePath)
  28. For kk = 0 To UBound(theStr)
  29. Set theBook = Workbooks.Open(theStr(kk))
  30. If err.Number = 0 Then
  31. On Error GoTo 0
  32. theBookCount = theBookCount + 1
  33. For Each theSht In theBook.Worksheets
  34. If WorksheetFunction.CountA(theSht.Cells) = 0 Then
  35. theVisibleShtCount = 0
  36. If theBook.Sheets.Count > 1 Then
  37. For Each sht In theBook.Sheets
  38. If sht.Visible = xlSheetVisible Then theVisibleShtCount = theVisibleShtCount + 1
  39. Next sht
  40. If theVisibleShtCount > 1 Then
  41. Application.DisplayAlerts = False
  42. theSht.Delete
  43. theShtCount = theShtCount + 1
  44. Application.DisplayAlerts = True
  45. Else
  46. If theSht.Visible = xlSheetVisible Then
  47. Application.ScreenUpdating = True
  48. MsgBox "工作簿内须至少含有一张可视工作表" _
  49. & vbNewLine & vbNewLine & "当前待处理工作簿“" & theBook.Name & "”的最后一个可视工作表“" & theSht.Name & "”不能被删除!", vbExclamation, "警告"
  50. Application.ScreenUpdating = False
  51. End If
  52. End If
  53. Else
  54. Application.ScreenUpdating = True
  55. MsgBox "工作簿内须至少含有一张可视工作表" _
  56. & vbNewLine & vbNewLine & "当前待处理工作簿“" & theBook.Name & "”的最后一个可视工作表“" & theSht.Name & "”不能被删除!", vbExclamation, "警告"
  57. Application.ScreenUpdating = False
  58. End If
  59. End If
  60. Next theSht
  61. theBook.Close SaveChanges:=True
  62. Else
  63. On Error GoTo 0
  64. Application.ScreenUpdating = True
  65. MsgBox "打开工作簿" & theStr & "失败!", vbCritical, "错误"
  66. Application.ScreenUpdating = False
  67. End If
  68. Next
  69. End If

  70. Application.ScreenUpdating = True
  71. MsgBox "共计处理 " & theBookCount & " 个工作簿,删除 " & theShtCount & " 个工作表", vbInformation, "提示"
  72. Application.ShowWindowsInTaskbar = True
  73. ' Else
  74. ' Application.ScreenUpdating = True
  75. ' MsgBox "不存在目标工作簿!", vbInformation, "提示"
  76. ' End If
  77. The_Exit:
  78. Application.ScreenUpdating = True
  79. Application.ShowWindowsInTaskbar = True

  80. 100:
  81. Application.ScreenUpdating = True
  82. MsgBox err.Description ' 是这句吗??????????????????????????????????????不行哦。

  83. Set theBook = Nothing
  84. End Sub
复制代码
回复

使用道具 举报

发表于 2015-2-11 21:45 | 显示全部楼层    本楼为最佳答案   
msgbox都不需要,处理文件的时候。
回复

使用道具 举报

 楼主| 发表于 2015-2-11 21:49 | 显示全部楼层
hwc2ycy 发表于 2015-2-11 21:45
msgbox都不需要,处理文件的时候。

全部注释掉还是会弹出对话框的。

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
    '    If theStr <> "" Then
    Application.ShowWindowsInTaskbar = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
   
    If theStr <> ThisWorkbook.Name Then
        On Error Resume Next
        theStr = GetName(thePath)
        For kk = 0 To UBound(theStr)
            Set theBook = Workbooks.Open(theStr(kk))
            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
        Next
    End If
    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
回复

使用道具 举报

 楼主| 发表于 2015-2-11 22:02 | 显示全部楼层
hwc2ycy 发表于 2015-2-11 21:45
msgbox都不需要,处理文件的时候。

http://www.excelpx.com/thread-339039-1-1.html
文件夹1下的工作簿,黄色单元格的行(空行)怎么删除错误?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-8 17:22 , Processed in 0.286862 second(s), 7 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表