提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何跨多个工作簿文件同时运行宏?

在本文中,我将讨论如何在不打开多个工作簿文件的情况下跨多个工作簿文件运行宏。 以下方法可以帮助您在Excel中解决此任务。

使用VBA代码在多个工作簿中运行宏


使用VBA代码在多个工作簿中运行宏

若要在不打开多个工作簿的情况下运行宏,请应用以下VBA代码:

1。 按住 ALT + F11 键打开 Microsoft Visual Basic for Applications 窗口。

2。 点击 插页 > 模块,并粘贴在下面的宏 模块 窗口。

VBA代码:同时在多个工作簿上运行相同的宏:

Sub LoopThroughFiles()
    Dim xFd As FileDialog
    Dim xFdItem As Variant
    Dim xFileName As String
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
        xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
        xFileName = Dir(xFdItem & "*.xls*")
        Do While xFileName <> ""
            With Workbooks.Open(xFdItem & xFileName)
                'your code here
            End With
            xFileName = Dir
        Loop
    End If
End Sub

注意:在上面的代码中,请复制并粘贴您自己的代码,而不是 小组 标题和 END SUB 之间的页脚 使用Workbooks.Open(xFdItem和xFileName) 结束 脚本。 查看截图:

doc运行宏多个文件1

3。 然后按 F5 执行此代码的关键,和a 浏览 窗口显示,请选择一个文件夹,其中包含您要应用此宏的工作簿,请参见屏幕截图:

doc运行宏多个文件2

4. 然后点击 OK 按钮,所需的宏将立即从一个工作簿执行到其他工作簿。


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Caitlin Jarvis · 2 months ago
    Hi, firstly thank you for this macro, it was exactly what I was looking for. I do however have one problem, is there a way to close and save as each window as it completes. I have a large amount of files and I'm running out of RAM before the execution is complete.
    • To post as a guest, your comment is unpublished.
      skyyang · 2 months ago
      Hello, Caitlin ,
      Maybe the below code can help you, each time after running your specific code, a save file prompt box will pop out remind you to save the workbook.

      Sub LoopThroughFiles()
      Dim xFd As FileDialog
      Dim xFdItem As Variant
      Dim xFileName As String
      Dim xWB As Workbook
      Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
      If xFd.Show = -1 Then
      xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
      xFileName = Dir(xFdItem & "*.xls*")
      On Error Resume Next
      Do While xFileName <> ""
      Set xWB = Workbooks.Open(xFdItem & xFileName)
      With xWB
      'your code here
      End With
      xWB.Close
      xFileName = Dir
      Loop
      End If
      End Sub
    • To post as a guest, your comment is unpublished.
      Manvir Rai · 2 months ago
      Yes, Just add the below your following code if you wish it to save the file with the same name:

      'Saving the Workbook
      ActiveWorkbook.Save
  • To post as a guest, your comment is unpublished.
    Joel · 5 months ago
    Very useful macro, and it works great, but I would like to be able to select which files from that folder I want the macro to be ran on? For example I have 4 files in a folder with other excel files and I only want it ran on those 4 specific files. How can I tweak your macro to let me pick those 4 files from that folder?
    • To post as a guest, your comment is unpublished.
      skyyang · 5 months ago
      Hi, Joel,
      To trigger the same code in specific workbooks, you should apply the below code:

      Sub LoopThroughFiles()
      Dim xFd As FileDialog
      Dim xFdItem As Variant
      Dim xFileName As String
      Dim xFB As String
      With Application.FileDialog(msoFileDialogOpen)
      .AllowMultiSelect = True
      .Filters.Clear
      .Filters.Add "excel", "*.xls*"
      .Show
      If .SelectedItems.Count < 1 Then Exit Sub
      For lngCount = 1 To .SelectedItems.Count
      xFileName = .SelectedItems(lngCount)
      If xFileName <> "" Then
      With Workbooks.Open(Filename:=xFileName)
      'your code
      End With
      End If
      Next lngCount
      End With
      End Sub

      Please try it, hope it can help you!
  • To post as a guest, your comment is unpublished.
    yarto logistics · 5 months ago
    I followed instructions but get a compile error "Loop wihtout Do". What am i missing? My macro code is very simple just change font size of specified rows. Works by it self. Here is what I have... please help

    Sub LoopThroughFiles()
    Dim xFd As FileDialog
    Dim xFdItem As Variant
    Dim xFileName As String
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
    xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
    xFileName = Dir(xFdItem & "*.xls*")
    Do While xFileName <> ""
    With Workbooks.Open(xFdItem & xFileName)
    'your code here
    Rows("2:8").Select
    With Selection.Font
    .Name = "Arial"
    .Size = 12
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .Color = -11518420
    .TintAndShade = 0
    .ThemeFont = xlThemeFontNone
    End With
    xFileName = Dir
    Loop
    End If
    End Sub
    • To post as a guest, your comment is unpublished.
      skyyang · 5 months ago
      Hello, yarto,
      You missed the "End with" script at the end of your code, the correct one should be this:
      Sub LoopThroughFiles()
      Dim xFd As FileDialog
      Dim xFdItem As Variant
      Dim xFileName As String
      Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
      If xFd.Show = -1 Then
      xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
      xFileName = Dir(xFdItem & "*.xls*")
      Do While xFileName <> ""
      With Workbooks.Open(xFdItem & xFileName)
      'your code here
      Rows("2:8").Select
      With Selection.Font
      .Name = "Arial"
      .Size = 16
      .Strikethrough = False
      .Superscript = False
      .Subscript = False
      .OutlineFont = False
      .Shadow = False
      .Underline = xlUnderlineStyleNone
      .Color = -11518420
      .TintAndShade = 0
      .ThemeFont = xlThemeFontNone
      End With
      End With
      xFileName = Dir
      Loop
      End If
      End Sub

      Please try it, hope it can help you!
  • To post as a guest, your comment is unpublished.
    Iulia Curtman · 10 months ago
    Very useful macro, and it works fine, but I would like to be able to select which files from that folder I want the macro to be ran on? The files are not generated automatically in a separate folder, and I need to run different macros on each set of files from that folder, and then move them back in the initial folder.