从excel VBA添加行而不运行正在运行的文件

时间:2014-12-04 07:32:16

标签: vba excel-vba excel

我只需要IF行,如果他试图打开运行代码的文件(试图打开自己),那就跳过它。

这是我到目前为止的代码。

Sub Auto_Open()

        Dim SrcBook As Workbook
        Dim fso As Object, f As Object, ff As Object, f1 As Object

        Application.ScreenUpdating = False
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set f = fso.Getfolder("C:\test\new")
        Set ff = f.Files

        For Each f1 In ff
                Set SrcBook = Workbooks.Open(f1)
                Range("A2:IV" & Range("A20").End(xlUp).Row).Copy
                ThisWorkbook.Worksheets(1).Activate
                Range("A20").End(xlUp).Offset(1, 0).PasteSpecial
                Application.CutCopyMode = False
                SrcBook.Close
        Next
End Sub

2 个答案:

答案 0 :(得分:1)

按原样执行代码,可以将以下If语句添加到For Each-loop:

    For Each f1 In ff
       If StrComp(f1.Name, ActiveWorkbook.Name, vbTextCompare) <> 0 And _
          InStr(1, f1.Name, "~") = 0 Then
            Set SrcBook = Workbooks.Open(f1)
            Range("A2:IV" & Range("A20").End(xlUp).Row).Copy
            ThisWorkbook.Worksheets(1).Activate
            Range("A20").End(xlUp).Offset(1, 0).PasteSpecial
            Application.CutCopyMode = False
            SrcBook.Close
        End If
    Next

第一个条件阻止打开当前文件本身,第二个条件也会跳过Excel在打开文件时创建的临时文件。

重做代码

除此之外,我会重构和扩展代码如下:

Sub Auto_Open()
On Error GoTo Err_
    Dim fso As Object
    Dim Folder As Object
    Dim Files As Object
    Dim File As Object

    Application.ScreenUpdating = False
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Folder = fso.Getfolder("C:\Temp\Excel")
    Set Files = Folder.Files

    For Each File In Files
        If StrComp(File.Name, ActiveWorkbook.Name, vbTextCompare) <> 0 And _
           InStr(1, File.Name, "~") = 0 Then

            With Workbooks.Open(File.Path)
                Range("A2:IV" & Range("A20").End(xlUp).Row).Copy
                ThisWorkbook.Worksheets(1).Activate
                Range("A20").End(xlUp).Offset(1, 0).PasteSpecial
                Application.CutCopyMode = False
                .Close
            End With
        End If
    Next

Exit_:
    Application.ScreenUpdating = True
    Set Files = Nothing
    Set Folder = Nothing
    Set fso = Nothing
    Exit Sub

Err_:
    Resume Exit_
End Sub

一些评论:

  • 错误处理确保,即使出现错误 ScreenUpdating 又重新开启。否则,如果发生错误,您可以不向用户刷新应用程序。
  • 每个变量的单独行 - 更容易掌握
  • 更明确的变量名称。首先,代码应易于阅读,不易键入。
  • With -block for local variable,使其范围明确。同时保存局部变量SrcBook 在这里可能有人认为,该变量的名称有助于理解问题,最好保留。
  • 将对象变量显式设置为 Nothing 。可能是偏执狂,但作为SOP,它可以帮助避免在某些情况下出现各种奇怪的问题。

由于在 With -block中获取代码的意图可能具有挑战性,因此我甚至会进一步将该块提取到具有意图的新方法中 - 揭示名称,使事情更清晰。名称可以是CopyProductListFromFile

答案 1 :(得分:1)

 If Not ThisWorkbook.FullName = f1.Path Then
            Set SrcBook = Workbooks.Open(f1)
            Range("A2:IV" & Range("A20").End(xlUp).Row).Copy
            ThisWorkbook.Worksheets(1).Activate
            Range("A20").End(xlUp).Offset(1, 0).PasteSpecial
            Application.CutCopyMode = False
            SrcBook.Close
 End If