如果找不到文件,如何告诉代码跳过代码块?

时间:2018-12-21 19:17:11

标签: excel vba excel-vba

好的,所以我有一个代码,基本上需要将.csv文档拉到一个工作簿中并编辑一些小事情。我有一个代码,可以通过创建工作簿,从“暂存文件夹”中拖入文件然后自动调整/隐藏某些列/使第1行变为粗体来正常工作。如果所有8个潜在文件都可用,这就像一个超级按钮。但是,我们得到的.csv是Python程序的输出,范围可以是1-8个文件。 8个文件中的每个文件都有其自己的唯一名称,该名称在每个输出中均保持不变。例如:几何错误将始终提供名为几何错误的输出。根据发现的错误,输出将始终具有8个文件的某种组合。

问题是,如果我没有全部8个文件(通常不是),则VBA代码将无法正常工作。它的作用是在名为“ DVIEW Staging”的登台文件夹中查找,如果找不到该文件,则显示“ On Error Resume Next”行。见下文:

Application.DisplayAlerts = False
Set newbook = Workbooks.Add
ActiveWorkbook.SaveAs Filename:=Aname & "DVIEW Outputs.xlsx"

Environ ("USERPROFILE") + "\DVIEW Staging"
On Error Resume Next

Workbooks.Open Filename:= _
        Environ("USERPROFILE") & "\Desktop\DVIEW Staging\Geometry_Errors_Table.csv"
        On Error Resume Next
    Sheets("Geometry_Errors_Table").Move After:=Workbooks("DVIEW Outputs.xlsx").Sheets(1)
        Columns("A:Z").EntireColumn.AutoFit
        Range("A:A,B:B,C:C,I:I,J:J,K:K,L:L").Select
        Selection.EntireColumn.Hidden = True
        Rows("1:1").Select
        Selection.Font.Bold = True
        Range("D1").Select
        On Error Resume Next

Workbooks.Open Filename:= _
        Environ("USERPROFILE") & "\Desktop\DVIEW Staging\Fiber_and_Splice_Relationship_Errors.csv"
        On Error Resume Next
    Sheets("Fiber_and_Splice_Relationship_E").Move After:=Workbooks("DVIEW Outputs.xlsx").Sheets(1)
        Columns("A:Z").EntireColumn.AutoFit
        Range("A:A,C:C").Select
        Selection.EntireColumn.Hidden = True
        Rows("1:1").Select
        Selection.Font.Bold = True
        Range("B1").Select
        On Error Resume Next

因此,当找不到它时,它将运行CURRENT表(它实际找到的一个)上的所有hide和autofit命令。这意味着,如果输出文件夹中有1张纸,则可能会另外7次运行autofit / hide / bold。

我的问题是,如何获取它以阻止一段代码并在找不到文档的情况下跳过它?我只希望文件搜索下方的代码块能够在该文件上运行,或者根本不运行。换句话说,我不希望“几何错误”代码块隐藏Fiber_and_Splice_Relationship_Errors工作表上的7列,然后再运行Fiber_and_Splice_Relationship_Errors命令(因为我只希望为那一个隐藏2列)

很抱歉,它已经这么长了,我不知道如何简化就不简明扼要。

3 个答案:

答案 0 :(得分:1)

请尝试以下操作:

Option Explicit

Sub test()

    Dim FileName As String
    Dim Directory As String

    FileName = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value

    If Len(FileName) = 0 Then
        Exit Sub
    End If

    Directory = "C:\Users\mario\Desktop\Marios\" & FileName

    If Len(Dir(Directory)) = 0 Then
      MsgBox "File does not exist"

    End If

End Sub

答案 1 :(得分:1)

我终于找到答案了! Zack E使我走上了正确的道路,但这是让它按照我想要的去做的答案:

If MsgBox("This macro will combine DVIEW outputs. Do you wish to continue?", vbYesNo) = vbNo Then Exit Sub

    Dim FileGeoErrors As String
    Dim FileFiberAndSplice As String
Set newbook = Workbooks.Add
ActiveWorkbook.SaveAs Filename:=Aname & "DVIEW Outputs.xlsx"

FileGeoErrors = Environ("USERPROFILE") & "\Desktop\DVIEW Staging\Geometry_Errors_Table.csv"
FileFiberAndSplice = Environ("USERPROFILE") & "\Desktop\DVIEW Staging\Fiber_and_Splice_Relationship_Errors.csv"

If Dir(FileGeoErrors) <> "" Then
Workbooks.Open Filename:=FileGeoErrors
Sheets("Geometry_Errors_Table").Move After:=Workbooks("DVIEW Outputs.xlsx").Sheets(1)
            Columns("A:Z").EntireColumn.AutoFit
            Range("A:A,B:B,C:C,I:I,J:J,K:K,L:L").Select
            Selection.EntireColumn.Hidden = True
            Rows("1:1").Select
            Selection.Font.Bold = True
            Range("D1").Select
Else: GoTo 1
End If

1:
If Dir(FileFiberAndSplice) <> "" Then
Workbooks.Open Filename:=FileFiberAndSplice
Sheets("Fiber_and_Splice_Relationship_E").Move After:=Workbooks("DVIEW Outputs.xlsx").Sheets(1)
            Columns("A:Z").EntireColumn.AutoFit
            Range("A:A,C:C").Select
            Selection.EntireColumn.Hidden = True
            Rows("1:1").Select
            Selection.Font.Bold = True
            Range("B1").Select
Else: GoTo 2
End If

2:
If Dir(FileFiberCircuits) <> "" Then
Workbooks.Open Filename:=FileFiberCircuits
Sheets("Fiber_Has_Circuits").Move After:=Workbooks("DVIEW Outputs.xlsx").Sheets(1)
            Columns("A:Z").EntireColumn.AutoFit
            Range("A:A").Select
            Selection.EntireColumn.Hidden = True
            Rows("1:1").Select
            Selection.Font.Bold = True
            Range("B1").Select
Else: GoTo 3
End If

这里的关键是目录搜索,如果找到文件,它将执行代码块。如果不是,它将跳转到按顺序编号的下一个块。这样,它将循环浏览所有8个潜在的工作表,如果存在则运行代码,或者如果不存在则忽略代码。

答案 2 :(得分:0)

未经测试,但类似的方法可能有效,当然可以对其进行编辑以满足您的需求。

Sub Test()
    Dim FileGeoErrors As String
    Dim FileFiberAndSplice As String
    Dim GeoErrPath As String, FiberSplicePath As String
    Set newbook = Workbooks.Add

    ActiveWorkbook.SaveAs Filename:=Aname & "DVIEW Outputs.xlsx"

    Environ ("USERPROFILE") + "\DVIEW Staging"

    FileGeoErrors = Environ("USERPROFILE") & "\Desktop\DVIEW Staging\Geometry_Errors_Table.csv"
    FileFiberAndSplice = Environ("USERPROFILE") & "\Desktop\DVIEW Staging\Fiber_and_Splice_Relationship_Errors.csv"


    Workbooks.Open Filename:=FileGeoErrors
    Sheets("Geometry_Errors_Table").Move After:=Workbooks("DVIEW Outputs.xlsx").Sheets(1)
    GeoErrPath = FileGeoErrors
    ActiveSheet.Range("ZZ125") = GeoErrPath
        If Len(GeoErrPath) > 0 Then
            Columns("A:Z").EntireColumn.AutoFit
            Range("A:A,B:B,C:C,I:I,J:J,K:K,L:L").Select
            Selection.EntireColumn.Hidden = True
            Rows("1:1").Font.Bold = True
            Range("D1").Select
        End If
    Workbooks.Open Filename:=FileFiberAndSplice
    Sheets("Fiber_and_Splice_Relationship_E").Move After:=Workbooks("DVIEW Outputs.xlsx").Sheets(1)
    FiberSplicePath = FileFiberAndSplice
    ActiveSheet.Range("ZZ125") = FiberSplicePath
        If Len(FiberSplicePath) > 0 Then
            Workbooks.Open Filename:=FileFiberAndSplice
            Sheets("Fiber_and_Splice_Relationship_E").Move After:=Workbooks("DVIEW Outputs.xlsx").Sheets(1)
            Columns("A:Z").EntireColumn.AutoFit
            Range("A:A,C:C").Select
            Selection.EntireColumn.Hidden = True
            Rows("1:1").Font.Bold = True
            Range("B1").Select
        End If

End Sub