新浪博客

如何在WPS表格中将某xlsm文件的宏批量应用到其他xls文件

2025-03-27 15:22阅读:
Sub BatchApplyMacro()
Dim folderPath As String
Dim fileName As String
Dim targetWB As Workbook
Dim sourceWB As Workbook

Set sourceWB = ThisWorkbook ' 当前含宏的xlsm文件
folderPath = 'D:\12\' ' 修改为实际路径
fileName = Dir(folderPath & '*.xls') ' 匹配所有xls文件

Do While fileName <> ''
If fileName <> sourceWB.Name Then ' 排除自身文件
Set targetWB = Workbooks.Open(folderPath & fileName)
Application.Run ''' & sourceWB.Name & ''!Macro1' ' 替换为实际宏名
targetWB.Close SaveChanges:=True
End If
fileName = Dir()
Loop
End Sub

我的更多文章

下载客户端阅读体验更佳

APP专享