我怎样才能让我的循环移动到下一个工作簿而不在第一个工作簿中重复自己?

How can I make my loop move on to the next workbook without repeating itself in the first workbook?

我只想插入 3 行公式,但这些行在第一个打开的工作簿中一遍又一遍地插入。

为了便于讨论,使用 For Each 循环会更好吗?

Sub FXBuy()

Dim wb As Workbook

Dim PathName$, FileName$

    PathName = "H:\BASEL Reporting - Oliver's Mock\FX\"
    FileName = Dir(PathName)
    Do While FileName <> ""
    Set wb = Workbooks.Open(PathName & FileName)

wb.Sheets("FX").Columns("W:X").Hidden = True
wb.Sheets("FX").Columns("AD:AI").Hidden = True

wb.Sheets("FX").Columns("U:AB").SpecialCells(xlCellTypeVisible).Interior.Color = 65535
 

LastRow = wb.Sheets("FX").Columns("B").Find("B", LookAt:=xlPart).MergeArea.Row + wb.Sheets("FX").Columns("B").Find("B", LookAt:=xlPart).MergeArea.Rows.Count - 1

wb.Sheets("FX").Range("A" & LastRow & ":A" & LastRow + 2).EntireRow.Insert

wb.Sheets("FX").Range("S" & LastRow) = 20
wb.Sheets("FX").Range("S" & LastRow + 1) = 50

wb.Sheets("FX").Range("U" & LastRow).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.2,U:U$" & LastRow - 1 & ")"
wb.Sheets("FX").Range("Z" & LastRow).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.2,Z:Z$" & LastRow - 1 & ")"
wb.Sheets("FX").Range("AB" & LastRow).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.2,AB:AB$" & LastRow - 1 & ")"

wb.Sheets("FX").Range("U" & LastRow + 1).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.5,U:U$" & LastRow - 1 & ")"
wb.Sheets("FX").Range("Z" & LastRow + 1).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.5,Z:Z$" & LastRow - 1 & ")"
wb.Sheets("FX").Range("AB" & LastRow + 1).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.5,AB:AB$" & LastRow - 1 & ")"

wb.Sheets("FX").Range("U" & LastRow + 2).Formula = "=SUM(U" & LastRow & ",U" & LastRow + 1 & ")"
wb.Sheets("FX").Range("Z" & LastRow + 2).Formula = "=SUM(Z" & LastRow & ",Z" & LastRow + 1 & ")"
wb.Sheets("FX").Range("AB" & LastRow + 2).Formula = "=SUM(AB" & LastRow & ",AB" & LastRow + 1 & ")"

Loop

End Sub

您的代码将从 With wb.Sheets("FX") 语句中受益匪浅,因此您无需重复该语句 100 次。

您的问题是您需要在循环之前 FileName = Dir() 移动到下一个文件。

Public Sub FXBuy()
    Dim PathName As String
    PathName = "H:\BASEL Reporting - Oliver's Mock\FX\"
    
    Dim FileName As String
    FileName = Dir(PathName)
    
    Do While FileName <> vbNullString
        Dim wb As Workbook
        Set wb = Workbooks.Open(PathName & FileName)

        With wb.Worksheets("FX")  ' this is used by every statement that starts with a dot
            .Columns("W:X").Hidden = True
            .Columns("AD:AI").Hidden = True

            .Columns("U:AB").SpecialCells(xlCellTypeVisible).Interior.Color = 65535
 
            LastRow = .Columns("B").Find("B", LookAt:=xlPart).MergeArea.Row + .Columns("B").Find("B", LookAt:=xlPart).MergeArea.Rows.Count - 1

            .Range("A" & LastRow & ":A" & LastRow + 2).EntireRow.Insert

            .Range("S" & LastRow) = 20
            .Range("S" & LastRow + 1) = 50

            .Range("U" & LastRow).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.2,U:U$" & LastRow - 1 & ")"
            .Range("Z" & LastRow).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.2,Z:Z$" & LastRow - 1 & ")"
            .Range("AB" & LastRow).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.2,AB:AB$" & LastRow - 1 & ")"

            .Range("U" & LastRow + 1).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.5,U:U$" & LastRow - 1 & ")"
            .Range("Z" & LastRow + 1).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.5,Z:Z$" & LastRow - 1 & ")"
            .Range("AB" & LastRow + 1).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.5,AB:AB$" & LastRow - 1 & ")"

            .Range("U" & LastRow + 2).Formula = "=SUM(U" & LastRow & ",U" & LastRow + 1 & ")"
            .Range("Z" & LastRow + 2).Formula = "=SUM(Z" & LastRow & ",Z" & LastRow + 1 & ")"
            .Range("AB" & LastRow + 2).Formula = "=SUM(AB" & LastRow & ",AB" & LastRow + 1 & ")"
        End With
            
        FileName = Dir() ' move to next file
    Loop
End Sub

如果不同的工作簿有不同的工作表,您可以执行如下操作:

注意工作簿名称必须包含文件扩展名!

Public Sub FXBuy()
    Dim PathName As String
    PathName = "H:\BASEL Reporting - Oliver's Mock\FX\"
    
    Dim FileName As String
    FileName = Dir(PathName)
    
    Do While FileName <> vbNullString
        Dim wb As Workbook
        Set wb = Workbooks.Open(PathName & FileName)
        
        Dim ws As Worksheet
        Select Case wb.Name  ' workbook names must be with file extension
            Case "FX BUY.xslm":
                Set ws = wb.Worksheets("FX BUY")
            
            Case "other workbook.xslm":
                Set ws = wb.Worksheets("other worksheet")

            Case Else:  ' for all other workbooks use this default
                Set ws = wb.Worksheets("FX")
        End Select

        With ws  ' this is used by every statement that starts with a dot
            .Columns("W:X").Hidden = True
            .Columns("AD:AI").Hidden = True

            .Columns("U:AB").SpecialCells(xlCellTypeVisible).Interior.Color = 65535
 
            LastRow = .Columns("B").Find("B", LookAt:=xlPart).MergeArea.Row + .Columns("B").Find("B", LookAt:=xlPart).MergeArea.Rows.Count - 1

            .Range("A" & LastRow & ":A" & LastRow + 2).EntireRow.Insert

            .Range("S" & LastRow) = 20
            .Range("S" & LastRow + 1) = 50

            .Range("U" & LastRow).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.2,U:U$" & LastRow - 1 & ")"
            .Range("Z" & LastRow).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.2,Z:Z$" & LastRow - 1 & ")"
            .Range("AB" & LastRow).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.2,AB:AB$" & LastRow - 1 & ")"

            .Range("U" & LastRow + 1).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.5,U:U$" & LastRow - 1 & ")"
            .Range("Z" & LastRow + 1).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.5,Z:Z$" & LastRow - 1 & ")"
            .Range("AB" & LastRow + 1).Formula = "=SUMIF($K:$K$" & LastRow - 1 & ",0.5,AB:AB$" & LastRow - 1 & ")"

            .Range("U" & LastRow + 2).Formula = "=SUM(U" & LastRow & ",U" & LastRow + 1 & ")"
            .Range("Z" & LastRow + 2).Formula = "=SUM(Z" & LastRow & ",Z" & LastRow + 1 & ")"
            .Range("AB" & LastRow + 2).Formula = "=SUM(AB" & LastRow & ",AB" & LastRow + 1 & ")"
        End With
            
        FileName = Dir() ' move to next file
    Loop
End Sub

如果我没理解错的话,路径“H:\BASEL Reporting - Oliver's Mock\FX”中有多个 Excel 文件,您想要 运行 每个文件的代码其中

您的错误只是因为您从未更改“FileName”变量。

FileName = Dir(PathName) ' Find first file in path
Do While FileName <> ""  ' As long as that file is not nothing
Set wb = Workbooks.Open(PathName & FileName) ' Open it as an Excel.Workbook

我建议根本不要使用 Dir() 函数,因为它已被弃用。如果还想用,就得运行FileName = Dir()得到下一个文件名。当您在“循环”命令之前执行此操作时,它应该可以工作。