|
请见我附件“删除文件夹中其他文件中的隐藏表格”中模块1,为什么VBA中wb.Worksheets.Count的结果总是多1,而在立即窗口中执行这句结果是对的?
------------------------------------以下为代码------------------------------------------------------
Sub 删隐藏()
'删除其他工作薄中隐藏工作表
Application.ScreenUpdating = False
Application.DisplayAlerts = False
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "请选择工作簿所在的文件夹路径"
If .Show Then
Dim pathSelected As String, fn As String
pathSelected = .SelectedItems(1)
fn = Dir(pathSelected & "\*.xls*")
Dim wb As Workbook
Dim ws As Worksheet
Dim t As Integer
Dim i As Integer
Do While fn <> ""
Set wb = Workbooks.Open(pathSelected & "\" & fn, False)
If wb.Name <> ThisWorkbook.Name Then
t = wb.Worksheets.Count '就是这句有问题
MsgBox t
For i = 1 To t
If wb.Sheets(i).Visible <> xlSheetVisible Then
wb.Sheets(i).Visible = xlSheetVisible '如果不加这句,隐藏的工作表无法删除
wb.Sheets(i).Delete
End If
Next i
End If
wb.Save
wb.Close
fn = Dir
Loop
End If
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "已完成"
End Sub
|
|