Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1955|回复: 5

[已解决]如何用VBA批量删除制定文件夹中,多个Exce文件l的前两行

[复制链接]
发表于 2022-3-23 10:24 | 显示全部楼层 |阅读模式
2学分

最佳答案
2022-3-23 10:24
本帖最后由 limonet 于 2022-3-23 12:00 编辑

Sub RowsDelete()
    Dim MyPath$, Filename$
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show Then MyPath = .SelectedItems(1) Else Exit Sub
    End With
    Filename = Dir(MyPath & "\*.xls?")
    Do While Filename <> ""
        With Workbooks.Open(Filename:=MyPath & "\" & Filename)
            Rows("1:2").Delete
            .Close True
            Filename = Dir
        End With
    Loop
End Sub

事件1.zip

24.19 KB, 下载次数: 2

最佳答案

查看完整内容

Sub RowsDelete() Dim MyPath$, Filename$ With Application.FileDialog(msoFileDialogFolderPicker) If .Show Then MyPath = .SelectedItems(1) Else Exit Sub End With Filename = Dir(MyPath & "\*.xls?") Do While Filename "" With Workbooks.Open(Filename:=MyPath & "\" & Filename) Rows("1:2").Delete .Close True Filename = Dir ...
发表于 2022-3-23 10:24 | 显示全部楼层    本楼为最佳答案   
本帖最后由 limonet 于 2022-3-23 12:00 编辑

Sub RowsDelete()
    Dim MyPath$, Filename$
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show Then MyPath = .SelectedItems(1) Else Exit Sub
    End With
    Filename = Dir(MyPath & "\*.xls?")
    Do While Filename <> ""
        With Workbooks.Open(Filename:=MyPath & "\" & Filename)
            Rows("1:2").Delete
            .Close True
            Filename = Dir
        End With
    Loop
End Sub

RowsDelete.zip

13.82 KB, 下载次数: 5

回复

使用道具 举报

 楼主| 发表于 2022-3-23 10:28 | 显示全部楼层
如何用VBA批量删除指定文件夹中,多个Exce文件的前两行。
不好意思,标题有错别字
回复

使用道具 举报

发表于 2022-3-23 11:45 | 显示全部楼层
Sub RowsDelete()
    Dim MyPath$, Filename$
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show Then MyPath = .SelectedItems(1) Else Exit Sub
        Filename = Dir(MyPath & "\" & "*.xls?")
        Do While Filename <> ""
            With Workbooks.Open(Filename:=MyPath & "\" & Filename)
                Rows("1:2").Delete
                .Close True
                Filename = Dir
            End With
        Loop
    End With
End Sub

RowsDelete.zip

13.82 KB, 下载次数: 0

回复

使用道具 举报

 楼主| 发表于 2022-3-23 14:33 | 显示全部楼层
limonet 发表于 2022-3-23 10:24
Sub RowsDelete()
    Dim MyPath$, Filename$
    With Application.FileDialog(msoFileDialogFolderPic ...

谢谢您,解决大问题了!
回复

使用道具 举报

发表于 2022-3-23 14:55 | 显示全部楼层
kings00861 发表于 2022-3-23 10:28
如何用VBA批量删除指定文件夹中,多个Exce文件的前两行。
不好意思,标题有错别字
  1. Sub mydelete()
  2. Application.ScreenUpdating = False
  3. Dim myfile, x, wb As Workbook
  4. mypath = ThisWorkbook.Path & "\*.xls*"
  5. myfile = Dir(mypath)
  6. Do
  7.     If myfile <> ThisWorkbook.Name Then
  8.         Set wb = Workbooks.Open(ThisWorkbook.Path & "" & myfile)
  9.         wb.Worksheets(1).Rows("4:5").Delete
  10.         wb.Save
  11.         wb.Close False
  12.         Set wb = Nothing
  13.     End If
  14.     myfile = Dir
  15. Loop Until myfile = ""
  16. Application.ScreenUpdating = True
  17. End Sub
复制代码


回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-17 00:04 , Processed in 0.297087 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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