Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何转到下一个函数的循环中出现错误1004_Vba_Loops_For Loop - Fatal编程技术网

Vba 如何转到下一个函数的循环中出现错误1004

Vba 如何转到下一个函数的循环中出现错误1004,vba,loops,for-loop,Vba,Loops,For Loop,我编写了一个函数,在一个excel上导入2到12个excel文件。事实上,一天我有4个文件,另一天我可以有6个文件。不超过12个。我做了一个for循环来导入我的文件,但是如果我只有4个文件,当循环查找第5个文件时,它没有找到它,并且会弹出一个“error 1004”。我正在试图找到一种方法,使我的函数即使出现此错误也能继续运行。我想在循环后运行“宏2” Dim d As Integer For d = 2 To 13 Worksheets(d).Cells.ClearContents Ne

我编写了一个函数,在一个excel上导入2到12个excel文件。事实上,一天我有4个文件,另一天我可以有6个文件。不超过12个。我做了一个for循环来导入我的文件,但是如果我只有4个文件,当循环查找第5个文件时,它没有找到它,并且会弹出一个“error 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)
 .................................................
  .............................
  ............................................
   ........................
Dim文件\u路径为字符串
Dim文件\u agg作为工作簿
最后一排一样长
file\u path=“C:\Users\admin\Downloads\”
名称=工作表(1)。单元格(i,1)
设置file\u agg=Workbooks.Open(file\u path&Name&“.xlsx”,True,True)
长
N=文件\u agg.Sheets.Count'
Dim文件\u路径为字符串
Dim文件\u agg作为工作簿
最后一排一样长
file\u path=“C:\Users\admin\Downloads\”
名称=工作表(1)。单元格(i,1)
设置file\u agg=Workbooks.Open(file\u path&Name&“.xlsx”,True,True)
长

N=文件\u agg.Sheets.Count'快速且脏,可跳过导致错误的区域。。。 在开始一个问题循环之后,添加“On Error GoTo_u__;”并将其指向循环的末尾,如下所示:

For i = 2 To 12
On Error GoTo SkipToNext

---- all the troublesome code causing errors goes here ----

SkipToNext:

Next i 
更好的解决方案可能基于名称中的变量。可能在所有故障代码中加上:

If Name<>""
     ---- all the troublesome code causing errors goes here ----
End If
如果名称为“”
----所有导致错误的麻烦代码都在这里----
如果结束

希望其中一个对你有用

快速且肮脏地跳过导致错误的区域。。。 在开始一个问题循环之后,添加“On Error GoTo_u__;”并将其指向循环的末尾,如下所示:

For i = 2 To 12
On Error GoTo SkipToNext

---- all the troublesome code causing errors goes here ----

SkipToNext:

Next i 
更好的解决方案可能基于名称中的变量。可能在所有故障代码中加上:

If Name<>""
     ---- all the troublesome code causing errors goes here ----
End If
如果名称为“”
----所有导致错误的麻烦代码都在这里----
如果结束
希望其中一个对你有用

使用FileSystemObject(需要设置对ScriptingRuntime的引用以使用早期绑定和intellisense)。您可以使用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
使用FileSystemObject(需要设置对ScriptingRuntime的引用以使用早期绑定和intellisense)。您可以使用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
从任何一个尝试使用信息从任何一个尝试使用信息