【问题标题】:Error 1004 in for loop how to go to next function [duplicate]for循环中的错误1004如何转到下一个函数[重复]
【发布时间】:2020-02-21 05:48:29
【问题描述】:

我写了一个函数来在一个 excel 上导入 2 到 12 个 excel 文件。事实是一天我有 4 个文件,而另一天我可以有 6 个文件。永远不会超过 12 个。我做了一个 for 循环来导入我的文件,但是如果我只有 4 个文件,当循环查找第 5 个文件时它没有找到它并且弹出“错误 1004”。我正在尝试找到一种方法,即使我有这个错误,我的函数也能继续运行。我想在循环之后运行“宏 #2”。

Dim d As Integer

For d = 2 To 13

Worksheets(d).Cells.ClearContents

Next d


Dim i As Integer
For i = 2 To 12


Dim file_path As String
Dim file_agg As Workbook
Dim lastrow As Long

Name = Worksheets(1).Cells(i, 1)

file_path = "C:\Users\admin\Downloads\"



Set file_agg = Workbooks.Open(file_path & Name & ".xlsx", True, True)
lastrow = file_agg.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
file_agg.Sheets(1).Range("A1:Z" & lastrow).Copy ThisWorkbook.Sheets(i).Range("A1:Z" & lastrow)
file_agg.Close SaveChanges:=False


Next i       

'macro #2 (exemple)
 .................................................
  .............................
  ............................................
   ........................

【问题讨论】:

标签: vba loops for-loop


【解决方案1】:
Dim file_path As String
Dim file_agg As Workbook
Dim lastrow As Long
file_path = "C:\Users\admin\Downloads\"
Name = Worksheets(1).Cells(i, 1)
Set file_agg = Workbooks.Open(file_path & Name & ".xlsx", True, True)
dim n as long 
N=file_agg.Sheets.Count '<<<<
on error resume next 'this might work also with a fixed number 
For i = 2 To N
    lastrow = file_agg.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
    file_agg.Sheets(1).Range("A1:Z" & lastrow).Copy ThisWorkbook.Sheets(i).Range("A1:Z" 
    & lastrow) 'belongs to line above
    file_agg.Close SaveChanges:=False
Next i     

Sheets.Count

如果这个问题属于文件数,请查看 DIR() 函数。 使用占位符,他们将返回一个包含所有匹配文件的数组。 这也可以算 :) 在这种情况下,Ubound 是你的朋友。但是根据。您的代码只想导入一个文件,但不知道有多少张。

【讨论】:

    【解决方案2】:

    快速而肮脏地跳过导致错误的区域... 刚开始问题循环后,添加“On Error GoTo ___”并将其指向循环的末尾,如下所示:

    For i = 2 To 12
    On Error GoTo SkipToNext
    
    ---- all the troublesome code causing errors goes here ----
    
    SkipToNext:
    
    Next i 
    

    更好的解决方案可能基于您的 Name 变量中的内容。也许将所有的故障代码框起来:

    If Name<>""
         ---- all the troublesome code causing errors goes here ----
    End If
    

    希望其中一个对你有用!

    【讨论】:

      【解决方案3】:

      使用 FileSystemObject(您需要设置对 ScriptingRuntime 的引用才能使用早期绑定和智能感知)。您可以使用带有 For Each 构造的 FileSystemObject 循环文件夹中的所有文件。

      Sub LoopFilesInFolder()
          Dim FolderPath As String
          FolderPath = "C:\Users\admin\Downloads\"
          Dim fso As FileSystemObject
          Set fso = New FileSystemObject
          With fso
              Dim fldr As Folder
              Set fldr = .GetFolder(FolderPath)
          End With
          Dim fl As File
          For Each fl In fldr.Files
      
          Next
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2021-08-27
        • 2016-03-04
        • 1970-01-01
        • 2019-09-01
        • 2015-07-28
        • 1970-01-01
        • 1970-01-01
        • 2020-12-05
        • 2021-11-15
        相关资源
        最近更新 更多