本人菜鸟,想批量删除五十多个excel,第二个sheet页的第二行的内容,在贴吧里找到如下:
Sub test() Application.ScreenUpdating = False p = "d:\test\" '要删除行的文件所在目录根据实际修改 f = Dir(p & "*.xls") Do While f <> "" Set w = Workbooks.Open(p & f) w.Sheets(1).Rows(2).Delete w.Close True End If f = Dir Loop Application.ScreenUpdating = Truemsgbox "搞定,请勿重复运行!"End Sub
-- 然后本人修改为:
Set ExcelApp = CreateObject("Excel.Application")
Call test
Sub test()
msgbox "Ready? GO!"
ExcelApp.ScreenUpdating = False
p = "F:\【Folder】\【00】\TestFolder" '要删除行的文件所在目录根据实际修改
f = Dir(p & "*.xls")
Do While f <> ""
Set w = Workbooks.Open(p & f)
w.Sheets(2).Rows(2).Delete
w.Close True
f = Dir
Loop
ExcelApp.ScreenUpdating = True
msgbox "搞定,请勿重复运行!"
End Sub
注:Range("A1").EntireColumn.Delete 这样是删除A1整列
Range("A1").EntireRow.Delete 这样是删除A1整行
Range("A1").Delete 这是删除A1单元格 A2单元格会移上去
上述命令就好像 你选中A1单元格后,右击选择 -》删除 ,在弹出对话框中有四个选项
p = "F:\【Folder】\【00】\TestFolder\" 写了也不好使,还是那个截图的错误。。。求大神助我一臂之力!
追答最好把数据复制一份
你进入VBE编辑器后,新建一个模块,把下面代码粘贴进去,然后单击“工具”菜单下“引用”找到 Microsoft Script Runtime 这项打上勾就ok了,再把 Set fd = fso.GetFolder("C:\Documents and Settings\XXF\桌面\新建文件夹")这句里面的换成你文件夹的路径,我已经测试过没问题
Sub DeleteRow()
Dim fso As New FileSystemObject
Dim fd As Folder
Dim f As file
Dim book As Workbook
Set fd = fso.GetFolder("C:\Documents and Settings\XXF\桌面\新建文件夹")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each f In fd.Files
If f.Type = "Microsoft Excel 工作表" Then
' Debug.Print f.Name
Set book = Application.Workbooks.Open(f.Path)
book.Worksheets(2).Rows(2).Delete
book.Close SaveChanges:=True
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub