1、复制如下代码执行即可。
3、
Dim arrf(), mf&
Sub VBA代码一次性删除同一个目录下的所有xls文件所有名称()
Dim Fso As Object, i&, sFileType$, na As Name
Application.ScreenUpdating = False
Set Fso = CreateObject("Scripting.FileSystemObject")
sFileType = "*.xls"
Call GetFiles(ThisWorkbook.Path, sFileType, Fso)
For j = 1 To mf
With Workbooks.Open(arrf(j))
For Each na In .Names '历遍打开的工作簿所有名称
na.Delete '删除名称
Next
.Close True '保存关闭
End With
Next
mf = 0
Erase arrf
Set Fso = Nothing
Application.ScreenUpdating = True
MsgBox "ok"
End Sub
Private Sub GetFiles(ByVal sPath$, ByVal sFileType$, Fso As Object)
Dim Folder As Object
Dim SubFolder As Object
Dim File As Object
Set Folder = Fso.GetFolder(sPath)
For Each File In Folder.Files
If File.Name Like sFileType Then
If File.Name <> ThisWorkbook.Name Then
mf = mf + 1
ReDim Preserve arrf(1 To mf)
arrf(mf) = sPath & "\" & File.Name
End If
End If
Next
If Folder.SubFolders.Count > 0 Then
For Each SubFolder In Folder.SubFolders
Call GetFiles(SubFolder.Path, sFileType, Fso)
Next
End If
Set Folder = Nothing
Set File = Nothing
Set SubFolder = Nothing
End Sub
本回答被网友采纳