excel 为当前打开的所有文件应用1个宏vba?

ioekq8ef  于 2023-01-10  发布在  其他
关注(0)|答案(2)|浏览(229)

因为所有我有一个重复的工作,我需要每天做。
因为我是一个完整的noob和我的完整的宏列表,我已经创建了阅读这里,也许你可以帮助我一个宏
是否有可能从下面的宏改变,它将适用于所有已打开的工作簿?

Sub copyDown()

Dim myCount As Double

myCount = WorksheetFunction.CountA(Range("B:B"))

Range("ab2:ad" & myCount).FillDown

End Sub
Sub columnA()
    
    Dim myfirstRow, myLastrow As Integer
    
    myfirstRow = WorksheetFunction.CountA(Range("A:A")) + 1
    myLastrow = WorksheetFunction.CountA(Range("B:B"))
    
    Range("a" & myfirstRow & ":a" & myLastrow).Formula = "=TODAY() - 1"
    Range("a" & myfirstRow & ":a" & myLastrow).Select
    Selection.Copy
    
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    
End Sub

这两个子我需要做的5个文件,其中每个文件有2个特定的表
理论上,我知道我可以安排它有点不同,但我原来的宏有大约1600行,我害怕触及运行宏,我已经建立了2周:
谢谢大家

9udxz4iz

9udxz4iz1#

下面介绍如何创建一个单独的子函数,它将查找每个工作簿,然后调用原始子函数,并将它应该处理的工作表传递给它。
我只为“CopyDown”做过,但过程完全一样。

Option Explicit
Sub Iterate_Workbooks()

    Dim WB As Workbook
    
    For Each WB In Application.Workbooks
        ' This is a way to exclude open workbooks from your search
        ' OR remove the "not" to include withbooks with only certain 
        '   text in their name. "*" is wildcard, see some examples:
        'If Not WB.Name Like "Master*" Then
        'If WB.Name Like "FillerBook # *" Then
        If Not WB.Name Like "*.xlsm" Then
            Call copyDown(WB.Worksheets(1))
        End If
    Next WB
    
End Sub
Sub copyDown(WS As Worksheet)

    Dim myCount As Double
    
    With WS
        myCount = WorksheetFunction.CountA(.Range("B:B"))
        .Range("ab2:ad" & myCount).FillDown
    End With

End Sub

z9smfwbn

z9smfwbn2#

你可以创建一个函数来遍历所有打开的工作簿,然后从每个工作簿遍历所有的工作表,并匹配工作表的名称,通过传递工作表引用来调用你的子例程columnA和copyDown,希望这能有所帮助!

Sub ProcessAllWorkbooks()
    Dim WB As Workbook, WS As Worksheet
    
    For Each WB In Workbooks
        For Each WS In WB.Sheets
            If UCase(WS.Name) = "WHATEVER_NAME_OF_COPY_DOWN_SHEET_IN_UPPERCASE" Then
                Call copyDown(WS)
            ElseIf UCase(WS.Name) = "WHATEVER_NAME_OF_COLUMNa_SHEET_IN_UPPERCASE" Then
                Call columnA(WS)
            End If
        Next
    Next

End Sub

Sub copyDown(processWS As Worksheet)
Dim myCount As Double
    
    With processWS
        .Activate
        .Range("B1").Select
        myCount = WorksheetFunction.CountA(Range("B:B"))
        Range("ab2:ad" & myCount).FillDown
    End With
End Sub

Sub columnA(processWS As Worksheet)
    
    Dim myfirstRow, myLastrow As Integer
    
    With processWS
        .Activate
        .Range("A1").Select
        myfirstRow = WorksheetFunction.CountA(Range("A:A")) + 1
        myLastrow = WorksheetFunction.CountA(Range("B:B"))
        
        Range("a" & myfirstRow & ":a" & myLastrow).Formula = "=TODAY() - 1"
        Range("a" & myfirstRow & ":a" & myLastrow).Select
        Selection.Copy
        
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
    End With
End Sub

相关问题