vba - 对任务栏中打开的所有文件一一运行宏

标签 vba excel excel-2010

我的工作是每天格式化 100 个文件。虽然我有一个为此目的而设计的宏,但我必须在保存前一个文件后对每个文件运行宏。

我的问题是我如何能够一步在这些打开的工作簿上运行我的宏。当我保存一个时,它将在队列中的另一个上运行。

最佳答案

将以下宏作为 Passerby 放入“BASE”工作簿中提及

Sub SO()
    Dim macroList As Object
    Dim workbookName As String
    Dim wbFullPath
    Dim macroName As String
    Dim currentWb As Workbook
    Dim masterWb As Workbook ' the Excel file you are calling this procedure from
    Dim useWbList As Boolean
    Dim height As Long, i As Long
    Dim dataArray As Variant
    useWbList = False  ' DEFINE which input method
    Set macroList = CreateObject("Scripting.Dictionary")

    If useWbList Then
        ' you can also from the dictionary from 2 columns of an excel file , probably better for management
        With masterWb.Worksheets("Sheet1") '<~~ change Sheet1 to the sheet name storing the data
            height = .Cells(.Rows.Count, 1).End(xlUp).Row ' Assume data in column A,B, starting from row 1
            If height > 1 Then
                ReDim dataArray(1 To height, 1 To 2)
                dataArray = .Range(.Cells(1, 1), .Cells(height, 2)).Value
                For i = 1 To height
                    macroList.Add dataArray(i, 1), dataArray(i, 2)
                Next i
            Else
                'height = 1 case
                macroList.Add .Cells(1, 1).Value, .Cells(1, 2).Value
            End If
        End With
    Else
        ' ENTER THE FULl PATH in 1st agrument below,       Macro Name in 2nd argument
        ' Remember to make sure the macro is PUBLIC, try to put them in Module inside of Sheets'

        macroList.Add "C:\Users\wangCL\Desktop\Book1.xlsm", "ThisWorkbook.testing"
        'macroList.Add "FULL PATH", "MACRO NAME"
        'macroList.Add "FULL PATH", "MACRO NAME"
        'macroList.Add "FULL PATH", "MACRO NAME"
    End If

    Application.DisplayAlerts = False

    For Each wbFullPath In macroList.keys
        On Error GoTo 0
        macroName = macroList.Item(workbookName)
        workbookName = Mid(wbFullPath, InStrRev(wbFullPath, "\") + 1)
        Err.Clear
        On Error Resume Next
        Set currentWb = Nothing
        Set currentWb = Workbooks(workbookName) ' see if the workbook is already open

        If Err.Number <> 0 Then
            ' open the workbook if workbook NOT opened
            Set currentWb = Workbooks.Open(workbookName, ReadOnly:=True)
        End If
        On Error GoTo 0

        ' run the macro
        Application.Run workbookName & "!" & macroList.Item(wbFullPath)


        'close the workbook after running the macro
        currentWb.Close saveChanges:=False
        Set currentWb = Nothing
    Next wbFullPath
End Sub

希望对您有所帮助,如果有任何不清楚的地方请告诉我

关于vba - 对任务栏中打开的所有文件一一运行宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14374631/

相关文章:

excel - 如何使用 FormatConditions 根据单元格值与其他单元格的比较来更改单元格颜色?

python - 从 Python : macros may be disabled 运行 VBA 代码

c# - f# 读取 xls 文件 - 如何解析 value2 对象

python - 在 excel 中写入 csv 文件时,特殊字符无法正确显示

vba - 上周一的日期 Excel

ms-access - 最后插入行的自动编号值 - MS Access/VBA

excel - 如何使用 vba 在 Excel 中制作蒙版?

excel - 动态工作表索引号

c# - excel 2010 文件的 XlFileFormat

excel - 更改两个不同间隔的数字